Refit: Set the accept header by default or from settings

Created on 22 Nov 2015  路  10Comments  路  Source: reactiveui/refit

Many providers support json but serve xml by default. It's strange to decorate every interface with [Headers("Accept: application/json")] to achieve default behavior.

Most helpful comment

@aliabdoli If you really want to do it, it might be okay under the following condition:

Before automatically adding an accept header to the request, you need to be sure that we're going to be deserializing the response as JSON.

That means it can't happen in any of the following situations:

  • there is a custom ContentSerializer set in RefitSettings
  • the method return type is Task, Task<string>, Task<Stream>, Task<HttpContent> or any other variation that won't result in JSON deserialization.

This info may be available already as a single property on the RestMethodInfo. I can't remember. If not, it might warrant adding one.

Before you go ahead, let @onovotny weigh in and confirm he's okay with it too.

All 10 comments

Default behavior of what? I don't think refit should add any headers on it's own.

@Cheesebaron Refit doesn't support xml. You should be able to opt-out from it in those rare cases where a provider requires a custom accept header for json, otherwise I cannot see space for any ambiguity.

Most APIs these days are JSON only or JSON by default. If you have to use an accept header, it's not that hard to decorate the interface with it.

What you're asking for is a potentially breaking change for some people to save you one line of code.

This isn't that true for enterprise, especially Java world, they typically do support json but are xml-first. Also it isn't one line of code if you have several aspect-specific interfaces for a larger api. The main problem however is that it's counter-intuitive. It's standard that a library makes it clear what underlying format it recognizes (e.g. RestSharp). Also it would be extremely rare if it would break anything.

Can't you just set some DefaultRequestHeaders on a custom HttpClient instance? I don't think that Refit has to support such a "feature"...

Like @MKuckert stated any default header should go into the: HttpClient.DefaultRequestHeaders

var configuredHttpClient = new HttpClient();
configuredHttpClient.DefaultRequestHeaders.Add("Accept", "application/json")
///or
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue( "application/json"))
//then
var acceptanceService = RestService.For<IAcceptanceService>(configuredHttpClient);

await acceptanceService.AcceptMe();

It seems this ticket has been around for a while. So, wondering if anyone is working on it? otherwise, can I work on it and contribute?

@aliabdoli If you really want to do it, it might be okay under the following condition:

Before automatically adding an accept header to the request, you need to be sure that we're going to be deserializing the response as JSON.

That means it can't happen in any of the following situations:

  • there is a custom ContentSerializer set in RefitSettings
  • the method return type is Task, Task<string>, Task<Stream>, Task<HttpContent> or any other variation that won't result in JSON deserialization.

This info may be available already as a single property on the RestMethodInfo. I can't remember. If not, it might warrant adding one.

Before you go ahead, let @onovotny weigh in and confirm he's okay with it too.

@bennor I m on it. I ll send the pr. thanks for help

Closing due to age. Please try Refit v6 and reopen if still an issue.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

hiraldesai picture hiraldesai  路  4Comments

ahmedalejo picture ahmedalejo  路  6Comments

martincostello picture martincostello  路  4Comments

mary-perret-1986 picture mary-perret-1986  路  5Comments

ColKrumpler picture ColKrumpler  路  3Comments