Fixed serialization of empty body #1952
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Subsystem
Client: JsonFeature/JsonSerializer
Motivation
When using
JsonFeature
the empty body (e.g. aget()
request without a body) gets serialized to{}
instead of an empty string. This causes problems with some backends which expect the body to be empty.For more context, let's assume we define an HttpClient with JsonFeature and a default
Content-Type
header like this:This gives us some more convenience because we don't need to add the Content-Type header to each
post()
/put()
call. However, now every request where we don't assign a body (e.g. a simpleget("https://...")
) gets turned into a request containing{}
in the body and having aContent-Type: application/json
header.This causes some backends to return an error because they expect an empty body for
get()
requests. Often, the backend isn't under our control, so we have to comply with the usual expection: No body inget()
should mean no body in the resulting HTTP request.Solution
With this PR,
JsonFeature
simply passes throughEmptyContent
. I think this really is the most sensible behavior, mapping the intent 1:1. It also removes theContent-Type
header because there really is no content.If someone wants to send an empty
{}
then that's still possible by explicitly setting e.g.body = Unit
(or any serializable object without attributes). At least, now it's explicit.If someone wants to send an empty body, but with
Content-Type
set (for obscure backend requirements), that's also still possible by customizing the serializer and using a special object as a marker.