RestSharp default Content-Type for / x -www-form-urlencoded app on POST

RestSharp does not seem to allow me to override the Content-Type for the mail request. I followed the directions we found here to no avail. I also tried manually setting the content type of the header to application / json via request.AddHeaders ("content-type", "application / json");

Examples of query execution:

private IRestResponse ExecuteRequest<T>(string resource, Method method, T model) { var client = CreateRestClient(); var request = new RestRequest(resource, method) { RequestFormat = DataFormat.Json }; var json = JsonConvert.SerializeObject(model); request.AddHeader("Accept", "application/json"); request.AddHeader("User-Agent", "Fiddler"); request.Parameters.Clear(); request.AddParameter("auth_token", _apiKey); request.AddParameter("application/json", json, ParameteType.RequestBody); return client.Execute(request); } 

Error message:

 { "error": { "code": 400, "message": "The request requires a properly encoded body with the 'content-type' header set to '['application/json']", "type": "Bad Request" } } 

Fiddler request source data:

 POST **omitted** HTTP/1.1 Accept: application/json, application/xml, text/json, text/x-json,text/javascript, text/xml User-Agent: RestSharp/105.0.1.0 Content-Type: application/x-www-form-urlencoded Host: **omitted** Content-Length: 51 Accept-Encoding: gzip, deflate Connection: Keep-Alive 

As you can see, the Content-Type request is still the application / x -www-form-urlencoded. Any ideas? (thanks in advance)

+6
source share
2 answers

This seems to be a misunderstanding of how RestSharp interprets the settings for mail requests. From John Sheikhan's post on google group:

If this is a GET request, you cannot have the body of the request and AddParameter adds the values ​​to the request string of the URL. If this is POST, you cannot enable POST and the serialized request authority, since they occupy the same space. You can do a multi-page POST body, but this is not very general. Unfortunately, if you create a POST, the only way to set the Querystring value of the URL is either by concatenating the string or UrlSegments:

 var key = "12345"; var request = new RestRequest("api?key=" + key); // or var request = new RestRequest("api?key={key}); request.AddUrlSegment("key", "12345"); 

My modified Execute request method, which now works, looks like this:

 private IRestResponse ExecuteRequestAsPost<T>(T model, string resource, Method method) { resource += "?auth_token={token}"; var client = CreateRestClient(); var request = new RestRequest(resource, method) { RequestFormat = DataFormat.Json }; var json = JsonConvert.SerializeObject(model); request.AddHeader("User-Agent", "Fiddler"); request.AddUrlSegment("token", _apiKey); request.AddParameter("application/json", json, ParameterType.RequestBody); return client.Execute(request); } 
+8
source

It sounds like you got it, but if you are interested in the alternative, one of my goals written by Flurl was to be more explicit where you want to put your options, but still do it with less code. In this case, the entire query will look something like this:

 var response = await baseUrl .AppendPathSegment(resource) .SetQueryParam("auth_token", _apiKey) .WithHeader("User-Agent", "Fiddler") .PostJsonAsync(model); 
+1
source

Source: https://habr.com/ru/post/982534/


All Articles