英文:
how to properly make a POST request using Java Apache HttpClient?
问题
以下是翻译好的内容:
我正在尝试在Java程序中使用Apache HttpClient5来使用一个Web API。
使用curl
进行简单的请求:
curl -X POST -H "x-api-user: d904bd62-da08-416b-a816-ba797c9ee265" -H "x-api-key: xxxxxxxxxxx" https://habitica.com/api/v3/user/class/cast/valorousPresence
我得到了预期的响应和效果。
使用我的Java代码:
URI uri = new URIBuilder()
.setScheme("https")
.setHost("habitica.com")
.setPath("/api/v3/user/class/cast/valorousPresence")
.build();
Logger logger = LoggerFactory.getLogger(MyClass.class);
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
HttpPost httpPost = new HttpPost(uri);
httpPost.addHeader(new BasicHeader("x-api-user", getApiUser()));
httpPost.addHeader(new BasicHeader("x-api-key", getApiKey()));
CloseableHttpResponse httpResponse = httpClient.execute(httpPost);
logger.info(httpResponse.toString());
return httpResponse.getCode();
当我运行Java调用时,我得到的输出是:
411 Length Required HTTP/1.0
我确信我没有正确构造POST调用,应该如何处理?我尝试过指定Content-Type,但没有效果。在代码中尝试设置Content-Length会导致编译错误(据我所知,这由HttpClient5在幕后处理)。
我所有使用HttpClient5的GET请求都正常工作。
英文:
I am trying to use a web API in a Java program using Apache HttpClient5.
Using a simple request with curl
:
curl -X POST -H "x-api-user: d904bd62-da08-416b-a816-ba797c9ee265" -H "x-api-key: xxxxxxxxxxx" https://habitica.com/api/v3/user/class/cast/valorousPresence
I get the expected response and effect.
Using my Java code:
URI uri = new URIBuilder()
.setScheme("https")
.setHost("habitica.com")
.setPath("/api/v3/user/class/cast/valorousPresence")
.build();
Logger logger = LoggerFactory.getLogger(MyClass.class);
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
HttpPost httpPost = new HttpPost(uri);
httpPost.addHeader(new BasicHeader("x-api-user",getApiUser()));
httpPost.addHeader(new BasicHeader("x-api-key", getApiKey()));
CloseableHttpResponse httpResponse = httpClient.execute(httpPost);
logger.info(httpResponse.toString());
return httpResponse.getCode();
The output I get when running the Java call is
411 Length Required HTTP/1.0
I'm sure I'm not constructing the POST call correctly, how should it be done? I've tried specifying Content-Type and that has no effect. Trying to set Content-Length in the code causes compilation errors (as I understand it, this is handled behind the scenes by HttpClient5).
All my GET requests using HttpClient5 work fine.
答案1
得分: 0
一个 `POST` 请求总是带有有效载荷(内容)。没有内容的 `POST` 请求是不寻常的,所以您确定没有遗漏任何内容吗?
您需要调用 `setEntity()` 方法来设置有效载荷,即使它是空的,因为正是实体(entity)设置了 `Content-Length` 头部。
例如,您可以调用 `httpPost.setEntity(new StringEntity(""))`,这会设置 `Content-Type: text/plain` 和 `Content-Length: 0`。
英文:
A POST
always has a payload (content). A POST
without content is unusual, so are you sure you didn't forget something?
You need to call setEntity()
to set the payload, even if it is empty, because it is the entity that sets the Content-Length
header.
E.g. you could call httpPost.setEntity(new StringEntity(""))
, which sets Content-Type: text/plain
and Content-Length: 0
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论