Comments (5)
Are you talking about the response body? The request body converter provides the media type for a request.
from retrofit.
No, I am talking about the request body.
Yes, the request body converter can provide the media type for a request. But in my case, I want Retrofit to give the parsed MediaType to the request body converter.
Here is a use case:
interface MyApi {
@POST("/foo")
@Headers({"Content-Type: application/xml"})
Call<Void> foo(@Body Book book);
@POST("/bar")
@Headers({"Content-Type: application/json"})
Call<Void> bar(@Body Book book);
}
For a call to foo
, the request body converter should serialize Book to XML.
For a call to bar
, it should serialize Book to JSON.
from retrofit.
This way of using headers is not at all common. If you want to select different media types and converters where the type information is not sufficient to disambiguate, we recommend using custom annotations which are available to the converter factories and then having the selected converter provide the associated media type. There is a sample of this pattern here: https://github.com/square/retrofit/blob/trunk/samples%2Fsrc%2Fmain%2Fjava%2Fcom%2Fexample%2Fretrofit%2FJsonAndXmlConverters.java
from retrofit.
The custom annotation would be redundant regarding built-in header annotations.
Also what should I do if somebody use the built-in header annotations in addition to my custom annotation? Notice that in this case, retrofit currently overrides the mediatype returned by the converter with the mediatype coming from header annotations. So the consumer would be able to put himself in a buggy behaviour.
Also surprised that you call uncommon telling the server that I am sending JSON using the header Content-Type.
from retrofit.
The custom annotation would be redundant regarding built-in header annotations.
Except specifying the content type is not the use case for method-level headers. That's the converter and RequestBody type's job.
Also what should I do if somebody use the built-in header annotations in addition to my custom annotation? Notice that in this case, retrofit currently overrides the mediatype returned by the converter with the mediatype coming from header annotations. So the consumer would be able to put himself in a buggy behaviour.
You let the server return a 4xx for sending the wrong media type. The person writing the service is responsible for setting up Retrofit correctly. I'm not sure what your role is here, are you solely an author of a converter and nothing else? As long as you return the proper media type it's not your problem if someone overrides it (which they also could do in an OkHttp interceptor).
Also surprised that you call uncommon telling the server that I am sending JSON using the header Content-Type.
What I called uncommon is providing this as method-level header instead of through the dedicated and intended mechanism as part of the RequestBody.
from retrofit.
Related Issues (20)
- Exception on api calling with Okhttp HOT 1
- ./> The first request to any backend takes as long as the connectTimeout is defined (5 seconds). Any subsequent calls go then really fast. Why is the first call waiting until the timeout exceeds before it succeeds?
- Unable to build project HOT 2
- [THANK YOU] Jake Wharton - you are the Godfather of the Android! HOT 2
- Feature Request - Ignoring methods in the interface with an annotation HOT 2
- jackson-databind 2.17 not compatible with Android SDK 21-25 HOT 1
- ClassCastException thrown from unused Retrofit services with R8 HOT 4
- An illegal reflective access operation has occurred HOT 2
- UndeliverableException with RxJava3 and Retrofit2 HOT 1
- Unable to create @Body converter for java.util.Map<java.lang.String, java.lang.Object> when using kotlinx.serialization Converter HOT 1
- Jackson dependency version breaks Android 21+ minimum contract HOT 2
- UnknownHostException in V 2.9.0 HOT 4
- Set logging interceptor based on API HOT 1
- Retrofit + Kotlin coroutines : get info of actual Java exception instead of just kotlin.Unit
- HttpException can't be initialized HOT 1
- Does adding ProGuard rules for Okio still require in 2.11.0? HOT 1
- How to keep generic type of paramters? HOT 1
- How to get the URL endpoint and the GET/POST method when an Exception occurs?
- KSP support for retrofit-response-type-keeper HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from retrofit.