简体   繁体   English

如何使用 C# 在 POST 请求中发送 json 请求正文数据

[英]How to send json request body data in POST request using C#

I am New in C#.我是 C# 的新手。 I want to send JSON request body in POST request using C#.我想使用 C# 在 POST 请求中发送 JSON 请求正文。

I want To get results from Rest URL but it's showing me status code 500.我想从 Rest URL 获得结果,但它显示状态码 500。

How can I format the request body so that I able to get results from the rest URL?如何格式化请求正文,以便能够从 rest URL 获得结果?

My Request body in JSON -->我在 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);

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. myData字符串已经是 JSON 字符串,因此无需再次序列化。

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");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在MVC5中使用C#将表单输入转换为json数组并作为http post请求发送到正文中 - How to Convert form inputs to json array and send it as http post request in body, using c# in mvc5 如何使用C#发送包含JSON正文的GET请求? - How to send a GET request containing a JSON body, using C#? C#WebService发送标准的发布请求正文,以JSON响应 - C# WebService send standard post request body, respond with JSON 如何使用C#在REST服务的json请求主体中添加POST数据 - How add the POST data in the json request body of REST services Using C# 如何在C#中存储post请求的Json Body - How to store Json Body of post request in C# 使用HttpClient和C#在发布请求上发送json - Send a json on a post request with HttpClient and C# 如何使用 JSON 数组发送 C# POST 请求 - How to send C# POST Request with JSON Array 使用httpclient在C#中使用json字符串发送带有json字符串的帖子请求和请求正文中的文件 - Sending a post request with json string and a file in the body of the request in C# using httpclient 如何在 C# 中使用 UDP 发送发布请求? - How to send a post request using UDP in C#? 如何在 C# 中使用 HTTP 请求发送 JSON GET 数据 - How to send JSON GET data with an HTTP request in C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM