Search code examples
javajodd

Using JoddHttp put request to send JSON with the .body() corrupts Chinese text


My method is as follows:

public String submitMaterials(String url,JSONObject params) {
    return HttpRequest
        .create("put", url)
        .mediaType(MediaType.APPLICATION_JSON_UTF8_VALUE)
        .body(params.toJSONString())
        .send()
        .bodyText();
}

The parameters I import: enter image description here

enter image description here

Please help me,thanks!


Solution

  • Don't use the mediaType, it is just a part of ContentType, that does not set the encoding. So just use the contentType() instead:

    return HttpRequest
            .create("put", url)
            .contentType(MediaType.APPLICATION_JSON_UTF8_VALUE)
            .body(params.toJSONString())
            .send()
            .bodyText();
    }
    

    Note that you can use 2-argument version of contentType that sends media type and the content:

            .contentType("application/json", "UTF8")
    

    Write version of mediaType method will be removed just not to confuse people. See the javadoc too.