I'm trying to talk to an API, which seems to be using a non-standard setup, here's the curl request that works:
curl -d '{"id" : "blah"}' -H "Content-Type: application/json" -X GET http://127.0.0.1:2796/api/0.8/testing/testme
This returns what I expect, however whenever I try to do this using RestSharp, the API returns an error stating that it received no json. Here's the code I'm trying to use.
var client = new RestClient(server);
var request = new RestRequest("/api/0.8/testing/testme", Method.GET);
request.RequestFormat = DataFormat.Json;
request.AddJsonBody(new {id = "blah"});
IRestResponse response = client.Execute(request);
var content = response.
Console.WriteLine("Response: " + content);
It's a GET request, but it needs to also send json data, so I'm bit confused as to how that works. Does it put it in the body, or in the URL?
Any advice or input would be greatly appreciated.
Thanks
Why are you posting JSON in a get request? That doesn't make sense.
Are you sure you don't mean that you are posting JSON and then receiving a JSON response?
My suggestion is to download a tool called "Postman" and try to get it working there first.
Try this:
var client = new RestClient("http://127.0.0.1:2796/api/0.8/testing/testme");
var request = new RestRequest(Method.POST);
request.AddHeader("Content-Type", "application/json");
request.AddParameter("undefined", "{\"id\" : \"blah\"}", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
Then you can read the response and interpret that into your project.
Using
request.AddParameter("","",ParameterType.RequestBody);
instead of
AddJsonBody
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.