简体   繁体   中英

How to integrate Shiprocket API in e-commerce application to create a custom order in ASP.NET MVC

I am integrating Shiprocket API in my e-commerce application it is working fine when I post a simple request, but when I POST a request with a Json list, it is showing an error:

Remote Server return an error 422 Unprocessable Entity

My code:

string postData = data;
byte[] byteArray = Encoding.UTF8.GetBytes(postData);

request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = byteArray.Length;

dataStream = request.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Close();

WebResponse response = request.GetResponse();

this.Status = ((HttpWebResponse)response).StatusDescription;
dataStream = response.GetResponseStream();

StreamReader reader = new StreamReader(dataStream);
string responseFromServer = reader.ReadToEnd();

reader.Close();
dataStream.Close();
response.Close();

I am using HttpClient instead of HttpWebRequest

static async Task<string> PostURI(Uri u, HttpContent c)
    {
        string tokenId = "XXXXX";

        var response = string.Empty;
        using (var client = new HttpClient())
        {
            client.DefaultRequestHeaders.Authorization
                     = new AuthenticationHeaderValue("Bearer", tokenId);

            HttpResponseMessage result = await client.PostAsync(u, c);
           // HttpResponseMessage response1 = client.PostAsync(u).Result;
            if (result.IsSuccessStatusCode)
            {                   
                response = result.StatusCode.ToString();                    

            }
        }          

       // Console.WriteLine(t.Result);
       // Console.ReadLine();

        return response;
    }

and Call this Method inside this

public void hitApi()
    {
        List<itemList> itmlst = new List<itemList>();
        itemList itm = new itemList();
        itm.name = "Apple I7";
        itm.sku = "AS0125";
        itm.units = 1;
        itm.selling_price = 25000;
        itm.discount = 0;
        itm.tax = 0;
        itm.hsn = 04520;
        itmlst.Add(itm);
        var json = JsonConvert.SerializeObject(itmlst);
        int subt = 900;
        int length = 10;
        int breadth = 15;
        int height = 10;
        int weight = 5;

        string postData = "{\"order_id\":\"23323535\",\"order_date\":\"2019-11-22 11:11\",\"pickup_location\":\"Home\",\"billing_customer_name\":\"Vinay Kumar yadav\",\"billing_last_name\":\"Hassan\",\"billing_address\":\"House 221B, New Madeygunj  Village\",\"billing_city\":\"Lucknow\",\"billing_pincode\":\"226020\",\"billing_state\":\"Uttar Pradesh\",\"billing_country\":\"India\",\"billing_email\":\"jaanu.2010@gmail.com\",\"billing_phone\":\"7545201020\",\"shipping_is_billing\":true,\"order_items\":" + json + ",\"payment_method\":\"Prepaid\",\"sub_total\":" + subt + ",\"length\":" + length + ",\"breadth\":" + breadth + ",\"height\":" + height + ",\"weight\":" + weight + "}";


        Uri u = new Uri("https://apiv2.shiprocket.in/v1/external/orders/create/adhoc");
        var payload = postData;

        HttpContent c = new StringContent(payload, Encoding.UTF8, "application/json");
        var t = Task.Run(() => PostURI(u, c));
        t.Wait();

        Response.Write(t.Result);
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM