title: How to add parameters to java.net.http.HttpClient GET request?
body: <p>We found the following example, which works:</p>
  <pre><code>import java.net.http.HttpClient;
  :
  private static final HttpClient httpClient = HttpClient.newBuilder().version(HttpClient.Version.HTTP_1_1)
          .connectTimeout(Duration.ofSeconds(TIMEOUT)).build();
  :
  public String getStuff(HashMap&lt;String,String&gt; params) {
      HttpRequest request = HttpRequest.newBuilder().GET().uri(URI.create(&quot;https://httpbin.org/get&quot;))
              .setHeader(&quot;User-Agent&quot;, &quot;My Agent v1.0&quot;)
              .build();
      
      HttpResponse&lt;String&gt; response = httpClient.send(request, HttpResponse.BodyHandlers.ofString());
     return response.body();
  }
  </code></pre>
  <p>The question is how do we get the params into the request?  We could manually put them in the URI by string manipulation, but this wont work for POST.</p>
  <p>We would expect a setParameter method similar to setHeader, but this doesnt exist (according to eclipse at least).</p>
  <p>For now I am doing it manually thusly:</p>
  <pre><code>    String uri = &quot;http://Somesite.com/somepath&quot;;
      if (params != null) {
          uri += &quot;?&quot;;
          for (String key : params.keySet()) {
              uri += &quot;&quot; + key + &quot;=&quot; + params.get(key);
            }
      }
  
      HttpRequest request = HttpRequest.newBuilder().GET().uri(URI.create(uri))
              .setHeader(&quot;User-Agent&quot;, agent)
              .build();
  </code></pre>
  <p>Presumably for POST ill have to manually construct a body with the post params structure.</p>