define request content type as application/json for default body param #12
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.
Currently, for requests with a (non Formdata) body param, body is being set to a given argument value but
ContentTypeis not defined so when providing an object that can be serialised to Json as value - it throw the following errorjava.lang.IllegalStateException: Fail to serialize body. Content has type: class ******** (Kotlin reflection is not available), but OutgoingContent expected. If you expect serialized body, please check that you have installed the corresponding feature(like 'Json') and set 'Content-Type' header.According to Ktor docs: "If you install the JsonFeature, and set the content type to application/json you can use arbitrary instances as the body, and they will be serialised as JSON".
So we might want to set
contentType(ContentType.Application.Json)in that case (might want to do that only if the body param type is not a String nor a subtype of OutgoingContent).Otherwise, the only option for consumer to avoid that error is to set a default ContentType for HttpClient requests:
defaultRequest { contentType(ContentType.Application.Json) }