Change AcceptSharp Accept Header

I use RestSharp for client side development. I also use Ruby Grape gem for my server side user API. Grape gem can do version control by setting Accept HTTP header fe to application/vnd.twitter-v1+json

And the test command through the console works fine

 curl -H Accept=application/vnd.twitter-v1+json /statuses/public_timeline 

But when I try to set the header for RestRequest, I get error 404 on the server.

I have no idea why this is. I found another problem that the server returns 406 error , but in my case 404.

How can I put a custom value for the Accept header?

+6
source share
1 answer

You can customize your own Accept header using the AddHeader method ...

 var client = new RestClient("http://example.com/api"); var request = new RestRequest("statuses/public_timeline", Method.GET); request.AddHeader("Accept", "application/vnd.twitter-v1+json"); var response = client.Execute(request); var json = response.Content; 

This should work if you want to deserialize JSON yourself.


If you want to use the general Execute<T> method, which performs automatic deserialization for you, you will run into problems ...

From the RestSharp documentation on deserialization :

RestSharp includes deserializers for XML and JSON processing. Upon receiving the response, RestClient selects the correct deserializer to use based on the type of content returned by the server. The default values ​​can be overridden (see setting). Supported built-in content types:

  • application / json - JsonDeserializer
  • application / xml - XmlDeserializer
  • text / json - JsonDeserializer
  • text / xml - XmlDeserializer
  • * - XmlDeserializer (all other content types are not specified)

This suggests that by default, if the response content type is not one of those listed, RestSharp will try to use the XmlDeserializer for your data. This is customizable, but with extra work.

+5
source

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


All Articles