Closed snedbalek-paylocity closed 1 month ago
We do not filter based on content type. The assumption is that your API returns a fixed content type based on the request, and if you have multiple you can overload the methods in your interface and add headers to force the content type one way or the other.
You can, however, filter on content type by writing a custom Converter.Factory
. Once the Converter
is created, it's handed a ResponseBody
instance which does contain the MediaType
that you can use to choose between two other real converters.
Thanks for the confirmation!
We had the issue partially on the input side as well. Since we were passing serialized json into @Body. So we decided to go raw with ResponseBody
and have a full control over it without use of the ScalarsConverterFactory
completely.
But in case we will need to add Scalars in the future, I'll remember your idea. Thanks!
For request body serialization you can use an annotation to disambiguate between two converters that otherwise support the same type. We have a sample showing JSON and XML selection: https://github.com/square/retrofit/blob/trunk/samples/src/main/java/com/example/retrofit/JsonAndXmlConverters.java
Yeah, I saw that one. It's just that I need it one way for serialization of request body, another way for serializing of response body. E.g. We have
@POST
fun requestJson(jsonBody: String): Unit
@GET
fun responseString(): String
So I'd still need to mix both of the approaches and creating a bit of a complexity. But thanks for suggestion anyway :)
Well I guess it ultimately depends on what you want.
If the choice is symmetric (i.e., the request body format always matches the response body format) then you can use the annotation exclusively to choose both the request and response converters.
If the choice is asymmetric (i.e., caller can choose arbitrary request format and server and choose arbitrary response format) then you are forced to go with the two mechanisms.
You can also use Retrofit's own annotations to choose, such as by looking at @Headers
for the Accept
which should dictate what format the server will respond with. You can also overwrite the Content-Type
in @Headers
which a delegating factory could look at to determine the request body format.
By the documentation of ScalarsConverterFactory, I'd expect the converter to be applied only on text/plain:
But that is ignored and it consumes even
application/json
as shown in this test case:The shared case won't fail if the factories are reversed:
But that has implications on body serialization. Is there a way how to restrict converter only to certain content type? Is this a bug? Should the docu be updated?