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); 
4
  • 2
    500 means internal server error. it means something wrong at the server end. Commented Mar 4, 2021 at 10:22
  • sir, it is a problem in request body I unable to send the proper format of the request body Commented Mar 4, 2021 at 10:24
  • Does this help? stackoverflow.com/questions/46044206/… Commented Mar 4, 2021 at 14:24
  • yes it helps for me Commented Mar 5, 2021 at 12:24

1 Answer 1

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"); 
Sign up to request clarification or add additional context in comments.

2 Comments

@Sunny why did you unaccepted the answer? It’s working for you and I invested my time for you, have some courtesy
sorry for that sir. It happens mistakenly and I am thankful for your time.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.