make a get request call using HttpClient in java

package com.zetcode;

import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.HashMap;

public class PostRequest {

    public static void main(String[] args) throws IOException, InterruptedException {

        var values = new HashMap<String, String>() {{
            put("name", "John Doe");
            put ("occupation", "gardener");
        }};

        var objectMapper = new ObjectMapper();
        String requestBody = objectMapper
                .writeValueAsString(values);

        HttpClient client = HttpClient.newHttpClient();
        HttpRequest request = HttpRequest.newBuilder()
                .uri(URI.create("https://httpbin.org/post"))
                .POST(HttpRequest.BodyPublishers.ofString(requestBody))
                .build();

        HttpResponse<String> response = client.send(request,
                HttpResponse.BodyHandlers.ofString());

        System.out.println(response.body());
    }
}

4.29
7
DarkDust 120 points

                                    package com.zetcode;

import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;

public class GetRequestJava11 {

    public static void main(String[] args) throws IOException, InterruptedException {

        HttpClient client = HttpClient.newHttpClient();
        HttpRequest request = HttpRequest.newBuilder()
                .uri(URI.create(&quot;http://webcode.me&quot;))
                .build();

        HttpResponse&lt;String&gt; response = client.send(request,
                HttpResponse.BodyHandlers.ofString());

        System.out.println(response.body());
    }
}

4.29 (7 Votes)
0
Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
httpclient java maven example java httpclient get example .HttpClient java how to call a rest api from java using httpclient httpclient java example project best httpclient for java httpclient java download Reusing httpclient java httpclient create java httpclient java 8 example how to use httpclient in java 8 HttpClient client java httpclient.get() method java httpclient get method java get() in httpclient in java 8 httpclient in java 8 java best httpclient to use httpclient javadoc httpclient.send java example make a get request call using httpclient in java httpclient in java java 8 httpclient example httpclient java get httpclient examples java java httpclient request httpclient put request java java httpclient put request java httpclient put example java httpclient util put httpclient tutorial java java java net http httpclient example java httpclient get how to use httpclient in java HttpClientRequest usage in java httpclient for java 7 using httpclient in java httpclient example tutorial java java httpclient maven java get http response -httpclient java.net.http.HttpClient; HttpClient client = new HttpClient(); java connect api using simple java httpclient java use HttpClient connect api using httpclient java httpclient example java java http client HttpClient read response httpclient java 8 HttpClient get response code java httpclient library java java rest client httpclient httpclient java example httpclient class in java java.net.http.HttpClient; example java.net.http.HttpClient call api using httpclient java httpclient.send java HttpClient request example java httpclient java 11 example using httpclient java java httpclient java HTTPClients httpclient java httpclient java tutorial java httpclient put method example java httpclient example
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source