java - 如何在初始化后执行 Unirest 请求

标签 java rest http unirest

所以我正在使用 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 个 header 。 我正在考虑创建一个实用程序类,在其中我可以传递 header 及其各自值的 HashMap。但是,我不能这样做,因为我知道如何添加 header 的唯一方法是在 jsonResponse 初始化期间。初始化变量后如何添加标题?或者如何在数组或 HashMap 中添加 header 。

最佳答案

您可以使用.headers(Map<String, String> headers)方法从 map 添加 header ,而不是重复 header 调用,因此您不需要实用程序方法。

关于java - 如何在初始化后执行 Unirest 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45947783/

相关文章:

java - JPQL,是否可以在选择不同的对象时创建对象的子集合?

java - Asynctask 中多线程意外停止

java - 将关联数组(Hashmap)作为参数传递给 xml rpc

javascript - 在使用 Promise.then AngularJS foreach 之后仅显示一条消息

google-app-engine - 将关系存储为对象化键与长 ID

javascript - 避免长 Angular $http 查询超时

java - 如何为整个 Activity 添加 ScrollView ?

java - 使用 inbuild 注入(inject)的 Jersey 自定义方法参数注入(inject)

php - Symfony 4 使用用户实体的基本 http 身份验证

web-services - Scala - Play : how to nicely handle WS get exception