0

I just started using the new HTTP client in Java and I'm unsure on how to pass parameters for a PUT request.

The specific request I'm dealing with requires an Authentication token and a parameter type.

  • I've successfully dealt with the Authentication token using .headers()
  • I tried to do the same thing with the type parameter but I get an error message that states I haven't passed a type field.
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());
5
  • 1
    Is the type parameter expected to be a header? isn't it a query parameter... URI.create("...?type=type1")? Commented May 11, 2021 at 8:24
  • Thank you so much... If I had multiple parameters, how would I do it? would it be like URI.create("...?param1 = ... ? param2 = ...) Commented May 11, 2021 at 9:00
  • 1
    ?type=type1&param2=value2&param3=value3&param4=value4 Commented May 11, 2021 at 9:03
  • Thank you so much... Is there a way I can turn your comments into an answer? Commented May 11, 2021 at 9:06
  • 1
    Your question needs to be made clearer first and then you can answer it. Commented May 11, 2021 at 9:09

1 Answer 1

1

As @ernest_k commented, we can pass parameters through by appending them to the end of the URL in this format: ?type=type1&param2=value2&param3=value3&param4=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());
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.