![](/img/trans.png)
[英]Using the jersey client to do a POST operation with request params and a request body
[英]Writing a POST request using a jersey client
我正在嘗試使用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錯誤的請求錯誤。 我要去哪里錯了???
您將Accept標頭添加了2次,並在代碼中錯過了Content-Type 。 添加以下代碼即可使用。
builder.type(MediaType.APPLICATION_JSON);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.