使用泽西客户端编写POST请求

时间:2016-07-01 15:54:18

标签: java json post jersey http-status-code-400

我尝试使用jersey客户端

实现以下curl请求
curl  -H "Content-Type:application/json"  -H "Authorization:Bearer 7est6xAiAYrhGmJyUkUKemz2yG_Qqn5RW5FCW1Iq1NLs6khyCMHQ" -X POST -d  @example.json http://api.com/v1/jobs/

这是 json

{
image_url : abc
}

这是球衣实施

WebResource resource = Client.create(new DefaultClientConfig()).resource("http://api.com/v1/jobs");

WebResource.Builder builder = resource.accept(MediaType.APPLICATION_JSON);
builder.accept(MediaType.APPLICATION_JSON);
builder.header(HttpHeaders.AUTHORIZATION, "Bearer 7est6xAiAYrhGmJyUkUKemz2yG_Qqn5RW5FCW1Iq1NLs6khyCMHQ");
String input = "{\"image_url\": \"abc\"}";
ClientResponse output =  builder.post(ClientResponse.class, input);

这给我一个400 Bad请求错误。我哪里错了???

1 个答案:

答案 0 :(得分:1)

您在代码中添加了2次Accept标头并错过了Content-Type。添加以下代码即可。

builder.type(MediaType.APPLICATION_JSON);
相关问题