我将以下字符串传递给 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());
}
最佳答案
关于java - JAVA中HttpClient请求的编码类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56701433/