1

I am New in C#. I want to send JSON request body in POST request using C#.

I want To get results from Rest URL but it's showing me status code 500.

How can I format the request body so that I able to get results from the rest URL?

My Request body in JSON -->

{"filter":{"labtestName":[{"labtestName":"Ada"}]}}

code that I tried

string data1 = "{\filter\":{\"labtestName\":[{\"labtestName\":\"Ada\"}]}}";
        var RestURL = "https://nort.co.net/v1api/LabTest/Hurlabtest";
        HttpClient client = new HttpClient();
        string jsonData = JsonConvert.SerializeObject(data1);
        client.BaseAddress = new Uri(RestURL);

        StringContent content1 = new StringContent(jsonData, Encoding.UTF8, "application/json");
        client.DefaultRequestHeaders.Add("apptoken", "72f303a7-f1f0-45a0-ad2b-e6db29328b1a");
        client.DefaultRequestHeaders.Add("usertoken", "cZJqFMitFdVz5MOvRLT7baVTJa+yZffc5eVoU91OqkMYl6//cQmgIVkHOyRZ7rWTXi66WV4tMEuj+0oHIyPS6hBvPUY5/RJ7oWnTr4LuzlKU1H7Cp68za57O9AatAJJHiVPowlXwoPUohqe8Ad2u0A==");
        HttpResponseMessage response = await client.PostAsync(RestURL, content1);
        var result = await response.Content.ReadAsStringAsync();
        var responseData = JsonConvert.DeserializeObject<LabtestResponseData>(result);
Sunny
  • 23
  • 5

1 Answers1

0

You are sending the wrong data to the method,

I have corrected it, you can refer to the below code. myData string is already a JSON string so there is no need to serialize it again.

string myData = "{\"filter\": {\"labtestName\": [{\"labtestName\": \"Ada\"}]}}";
//string data1 = "{\filter\":  {\"labtestName\": [{\"labtestName\": \"Ada\"}]}}";
var RestURL = "https://tcdevapi.iworktech.net/v1api/LabTest/HSCLabTests";
HttpClient client = new HttpClient();
//string jsonData = JsonConvert.SerializeObject(myData);
client.BaseAddress = new Uri(RestURL);

StringContent content1 = new StringContent(myData, Encoding.UTF8, "application/json");
Vivek Nuna
  • 25,472
  • 25
  • 109
  • 197