How to suppress Charset being automatically added to Content-Type in okhttp

橙三吉。 提交于 2019-12-18 19:01:02

问题


Consider the following code:

    OkHttpClient client = new OkHttpClient();

    MediaType mediaType = MediaType.parse("text/plain; charset=utf-8"); // [A]
    RequestBody body = RequestBody.create(mediaType, media);
    String[] aclHeader = "x-goog-acl:public-read".split(":");

    Request request = new Request.Builder()
            .addHeader("Content-Type", "text/plain") // [B]
            .addHeader(aclHeader[0], aclHeader[1])
            .url(url)
            .put(body)
            .build();

    Response response = client.newCall(request).execute();

I am accessing GCS from a client, with a previously signed URL.

Problem: It seems okhttp adds the charset declared for the body [A] to the URL as well (at least for text/plain), even though it is not declared in [B]. This messes up my signed URL and GCS returns 403 Forbidden.

  • If I remove the charset from [A], it is still added.
  • If I add the charset to the signed URL before I sign it, it works and GCS returns 200 OK.

But this is not as it should be. At least when working with signed URLs, these must be sent to the server exactly as declared.

I tried using the Apache http client (which I don't want to use in production as okhttpclient is already part of my installation) and that client does not expose this behavior:

        String[] aclHeader = "x-goog-acl:public-read".split(":");

        StatusLine statusLine = Request

                .Put(url)
                .addHeader("Content-Type", "text/plain")
                .addHeader(aclHeader[0], aclHeader[1])
                .bodyByteArray(media)

                .execute().returnResponse().getStatusLine();

Is there a way to suppress the behavior in okhttp, that it adds to the Content-Type or transfers the Content-Type within the body redundantly?


回答1:


I found the solution:

The following line is the culprit:

RequestBody body = RequestBody.create(mediaType, media);

create has 3 signatures for media:

  • String
  • byte[]
  • File

When I pass a String, it disregards the supplied mediaType and adds the charset to it. Even for image/jpeg it would send

image/jpeg; charset=utf-8

to the server.

Using byte[] or File suppresses that behavior.

I hope this helps you!

[Stupid me - for simplicity I gave it a String during testing, as I didn't care about the body ;-( ]



来源:https://stackoverflow.com/questions/25560601/how-to-suppress-charset-being-automatically-added-to-content-type-in-okhttp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!