Quantcast
Channel: Opinion: Shopify Community - Shopify APIs & SDKs
Viewing all articles
Browse latest Browse all 25503

api create an order in c# by Richard Crittenden

$
0
0

im trying to create an order  using the following methods but just seem to get bad request back are there any post examples out there?

~
        private string CreateOrder()
        {
            const string shopifyUrlCommand = "/admin/products.json";
            // string data = Properties.Resources.CreateOrder;
            string data = @"{""order"": {""line_items"": [{""variant_id"": 273811465,""quantity"": 1}]}}";

            //var result = PostRequest(shopifyUrlCommand,data);
            Uri uri = new Uri(Properties.Settings.Default.API_StoreName + shopifyUrlCommand);
          var result= GetPOSTResponse(uri, data);
            return result;
        }

        private string GetPOSTResponse(Uri uri, string data)
        {
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(uri);
            request.ContentType = "application/json";
            //  request.ContentType = "text/plain;charset=utf-8";
            request.PreAuthenticate = true;
            request.Credentials = GetCredential(uri.OriginalString);
            request.Method = "POST";


            UTF8Encoding encoding = new UTF8Encoding();
            byte[] bytes = encoding.GetBytes(data);

            request.ContentLength = bytes.Length;

            using (Stream requestStream = request.GetRequestStream())
            {
                // Send the data.
                requestStream.Write(bytes, 0, bytes.Length);
            }
            string result;

            using (var resp = (HttpWebResponse)request.GetResponse())
            {
                if (resp.StatusCode != HttpStatusCode.OK)
                {
                    string message = String.Format("Call failed. Received HTTP {0}", resp.StatusCode);
                    throw new ApplicationException(message);
                }

                var sr = new StreamReader(resp.GetResponseStream());
                result = sr.ReadToEnd();
            }
            return result;
        }


Viewing all articles
Browse latest Browse all 25503

Trending Articles