'C# Restsharp GET Method Request will show "No JSON object could be decoded"
I am using C# winform on Visual Studio 2019 and using RestSharp. I have tested WEBAPI function on POSTMAN with correct output.

So i try to implement on C# Winform. I dump POSTMAN code (C# Restsharp) as following:
var client = new RestClient("http://192.168.2.10:88/en/product/ajax_api_getProductInfoBatch");
var request = new RestRequest(Method.GET);
request.AddHeader("cache-control", "no-cache");
request.AddHeader("Connection", "keep-alive");
request.AddHeader("Content-Length", "21");
request.AddHeader("Accept-Encoding", "gzip, deflate");
request.AddHeader("Host", "192.168.2.10:88");
request.AddHeader("Postman-Token", "f1ff99c7-dc02-40cb-b87a-
a046cf106a96,e2b7d88d-cfc9-4c99-8117-ec48398e56ed");
request.AddHeader("Cache-Control", "no-cache");
request.AddHeader("Accept", "*/*");
request.AddHeader("User-Agent", "PostmanRuntime/7.18.0");
request.AddParameter("undefined", "{\n \"Item No\":\"3101\"\n}",
ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
and i revised
request.AddParameter("undefined", "{\n \"Item No\":\"3101\"\n}"
to
request.AddParameter("application/json", "{\"Item No\":\"3101\"}", ParameterType.RequestBody);
Therefore, the full code will be
private void Button1_Click(object sender, EventArgs e)
{
var client = new RestClient("http://192.168.2.10:88/en/product/ajax_api_getProductInfoBatch");
var request = new RestRequest(Method.GET);
//request.AddHeader("cache-control", "no-cache");
//request.AddHeader("Connection", "keep-alive");
//request.AddHeader("Content-Length", "22");
//request.AddHeader("Accept-Encoding", "gzip, deflate");
//request.AddHeader("Host", "192.168.2.10:88");
//request.AddHeader("Postman-Token", "86e877f8-5755-4e57-960a-eaa78b1e8b6c,da863548-65e0-4053-9cb3-b5760da94165");
//request.AddHeader("Cache-Control", "no-cache");
//request.AddHeader("Accept", "*/*");
//request.AddHeader("User-Agent", "PostmanRuntime/7.18.0");
//request.AddHeader("Content-Type", "text/plain");
request.AddParameter("application/json", "{\"Item No\":\"3101\"}", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
var content = response.Content;
textBox1.Text = content;
}
but it is still output "No JSON object could be decoded" after GET request.
Anyone share your experience will be appreciated. or there are other ways to do GET/POST on C#. thanks.!
Solution 1:[1]
You can use httpclient and pass parameters as stringcontent and type should be application/json for stringcontent. This is for post request
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Somnath Ghosh |
