Okhttp: Overwrite Content-Type

Created on 14 Dec 2015  Â·  10Comments  Â·  Source: square/okhttp

We got a server that only accepts Content-Type: application/json and not Content-Type: application/json; charset=utf-8;

OkHttp keeps generating application/json; charset=utf-8; when we do the following json post

MediaType type = MediaType.parse("application/json");
String json = "{\"APIRequests\"}";
Request request = new Request.Builder()
.url(apiURL)
.post(RequestBody.create(type, json))
.build();
Response response = client.newCall(request).execute();
String html = response.body().string();

and returns an error. Is there any posibility to overwrite the content-type and not use the generated one from MediaType.parse?

Server it's a public one and i do have no control over it

Most helpful comment

I'm faced with a similar issue when using okhttp in combination with Elasticsearch 6.1.2

The type MediaType.parse("application/x-ndjson"); is rejected by the server due to the included encoding. Is there an RFC which requires the encoding? The workaround by @swankjesse works fine.

All 10 comments

First, fix your server.

Second, work around the problem by manually converting the string to UTF-8 bytes with getBytes() and use the byte[] to create the response body.

I said server it's not mine, so i need to send proper content-type

Got it. Please report a bug against the server.

Ok thank you

On Tue, Dec 15, 2015 at 3:25 AM, Jesse Wilson [email protected]
wrote:

Got it. Please report a bug against the server.

—
Reply to this email directly or view it on GitHub
https://github.com/square/okhttp/issues/2099#issuecomment-164612015.

First, fix your server.
Got it. Please report a bug against the server.

@swankjesse We should have an option to disable/override this behavior. I'm sending custom Content-Type and Accept headers to the server (in my company) as a way to implement api versioning. This is currently messing with our endpoint calls.
Glad to help if you need.

The ResponseBody API that accepts a String is the one that’s opinionated about charsets on content types. If you want to do your own thing, just convert your string to bytes first and use that API.

The issue is old and closed, but I'll just point out that this was really unexpected when I set the Content-Type to the specific value the Layer API expects (a popular chat service for applications) and it got overwritten.

Though thankfully the OkHttp network interceptors exist and can override anything!

Bump

Stumbled into it today, cannot remove charset from request even by explicitly setting header Content-Type.
Please fix asap.

If this is troubling you, you don’t need to have OkHttp pick a charset for you. Just do this:

    String requestBody = ...
    Charset charset = ...
    byte[] bytes = requestBody.getBytes(charset);
    return RequestBody.create(contentType, bytes);

I'm faced with a similar issue when using okhttp in combination with Elasticsearch 6.1.2

The type MediaType.parse("application/x-ndjson"); is rejected by the server due to the included encoding. Is there an RFC which requires the encoding? The workaround by @swankjesse works fine.

Was this page helpful?
0 / 5 - 0 ratings