Skip to content

Conversation

@fullkomnun
Copy link

Currently, for requests with a (non Formdata) body param, body is being set to a given argument value but ContentType is not defined so when providing an object that can be serialised to Json as value - it throw the following error java.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)
}

@cromefire
Copy link
Owner

cromefire commented Feb 21, 2021

I think you can set the content type from open api, I'm currently not aware whethere there's a template check thingy with which we can check that but we should probably only default to json (I think that's also how swagger ui handles that for example), so we don't accidentally override anything.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants