【发布时间】:2018-02-07 10:08:21
【问题描述】:
所以我正在使用 java 中的 REST API。我得到了我的 POST 请求,但我的代码对我来说似乎有点低效,例如:
HttpResponse<JsonNode> jsonResponse = Unirest.post("http://httpbin.org/post")
.header("accept", "application/json")
.queryString("apiKey", "123")
.field("parameter", "value")
.field("foo", "bar")
.asJson();
HttpResponse<JsonNode> jsonResponse2 = Unirest.post("http://httpbin.org/post")
.header("accept", "application/json")
.header("accept1", "application/json")
.header("accept2", "application/json")
.header("accept3", "application/json")
.asJson();
我有两个发帖请求。但是,其中之一有 4 个标题。 我正在考虑创建一个实用程序类,我可以在其中传递带有各自值的标头的 HashMap。 但是,我不能这样做,因为我知道如何添加标头的唯一方法是在 jsonResponse 的初始化过程中。初始化变量后如何添加标题?或者如何在数组或哈希图中添加标题。
【问题讨论】: