没有从 HttpClient 获得完整的 HTTP 响应

Not getting full HTTP response from HttpClient

我似乎没有从 httpclient 获得完整的 json 响应。我正在打 api 我在本地 运行 就像这样:

curl -i -X POST http://localhost:8098/<api location> -F "files=@<filename>"

我的回复是这样的:

{"data":[<bunch of json>]

但是当我尝试使用 httpclients post 完全相同的文件时,我得到了这个响应:

{"data":[]}

我做错了什么?这是我的 java 代码。谢谢!

public CloseableHttpResponse submit (File file) throws IOException {
    CloseableHttpClient client = HttpClients.createDefault();
    HttpPost post = new HttpPost(API_LOCATION + API_BASE);
    MultipartEntityBuilder builder = MultipartEntityBuilder.create();
    builder.addBinaryBody("ISO xml file", file, ContentType.APPLICATION_OCTET_STREAM, file.getName());
    HttpEntity multipartEntity = builder.build();
    post.setEntity(multipartEntity);

    CloseableHttpResponse response = client.execute(post);
    System.out.println("response: " + IOUtils.toString(response.getEntity().getContent(),"UTF-8"));
    client.close();
    return response;
}

正如 Andreas 和 Danilo 在评论中提到的那样:

In curl you name the field files, but in Java you name it ISO xml file. Since server only looks for files, it see nothing, and responds with nothing. -

What about the parameter's name? On curl you used 'files' and on http client you used 'ISO xml file'. Try changing it to 'file'. -

我需要将“ISO xml 文件”更改为“文件”并且成功了。