我刚开始在 Java 中使用 new HTTP client,但不确定如何为 PUT 请求传递参数。
我正在处理的特定请求需要一个 Authentication
令牌和一个参数 type
。
Authentication
成功处理了 .headers()
令牌type
参数执行相同的操作,但收到一条错误消息,指出我没有传递 type
字段。HttpClient client = HttpClient.newBuilder().build();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("...")) # The API url
.headers("Authorization", token, "type", "type 1")
.POST(HttpRequest.BodyPublishers.noBody())
.build();
HttpResponse<String> response = client.send(request,HttpResponse.BodyHandlers.ofString());
System.out.println(response.body());
答案 0 :(得分:0)
正如@ernest_k 所评论的,我们可以通过以这种格式将参数附加到 URL 的末尾来传递参数:?type=type1¶m2=value2¶m3=value3¶m4=value4
HttpClient client = HttpClient.newBuilder().build();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("..." + "?type=type 1"))
.headers("Authorization", token)
.POST(HttpRequest.BodyPublishers.noBody())
.build();
HttpResponse<String> response = client.send(request,HttpResponse.BodyHandlers.ofString());
System.out.println(response.body());