gpt4 book ai didi

java - JAVA中HttpClient请求的编码类型

转载 作者:行者123 更新时间:2023-12-02 01:27:22 27 4
gpt4 key购买 nike

我将以下字符串传递给 http-post-request:

字符串主体=

{
"invoicee": {
"customer": {
"type": "company",
"id": "a62117d6-d324-0ae8-8f69-bb63ada0fee2"
}
},
"department_id": "fba7d16b-a988-0420-894f-444b033ac379",
"payment_term": {
"type": "cash"
},
"grouped_lines": [
{
"section": {
"title": ""
},
"line_items": [
{
"quantity": 3,
"description": "An awesome product",
"unit_price": {
"amount": 123.3,
"currency": "EUR",
"tax": "excluding"
},
"tax_rate_id": "23d7af10-427b-06e4-8242-88e8228cc381"
}
]
}
],
"custom_fields": [
{
"id": "673bfdeb-1112-0423-9e54-a8adace28ae4",
"value": "Opdrachtbon Kruidvat 8957 365690.01 Kruidvat 8957 Neerstraat 34 Brakel (OVL) De keukenkraan is eraf gevallen, robinet cuisine est tombé "
}
]
}
        headers.add("Content-Type", "application/json");

headers.add("authorization", "Bearer " + acces_token);

它返回了 400 错误请求,其中包含表明这是格式错误的 JSON 的具体信息。

我的 header 包含:“Content-type”:“application/json”。

问题出在 custom_fields 对象中,“值”中存在以下字符“é”。用“e”替换这个字符使 json 正常,我的响应状态为 201 OK。

您知道我如何在以后的请求中防止这种情况发生吗?我是否必须为我的请求设置特定的编码?

ATM 我的请求如下所示:

        HttpEntity<String> requestEntity = new HttpEntity<String>(body, headers);

try {
rest.exchange("https://api.teamleader.eu/invoices.draft", HttpMethod.POST, requestEntity, String.class);
} catch (HttpClientErrorException e) {
System.out.println("!!ERROR!!");
System.out.println(e.getResponseBodyAsString());

}

最佳答案

您可能需要在 header 中设置字符集

内容类型:application/json;字符集=utf-8

这进一步解释了它 W3C Internationalisation

关于java - JAVA中HttpClient请求的编码类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56701433/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com