简体   繁体   中英

How to send DELETE request using HttpClient

how can I send an http DELETE request to this

using HttpClient object or something similar?

This is my code for GET and POST requests:

public class Main {
    public static void main(String[] args) throws IOException, InterruptedException {
        //jsonGetRequest();
        //jsonPostRequest();
    }

    public static void jsonGetRequest() throws IOException, InterruptedException {
        final String URL = "https://vbzrei5wpf.execute-api.us-east-1.amazonaws.com/test/pets";
        HttpClient httpClient = HttpClient.newHttpClient();
        HttpRequest httpRequest = HttpRequest
                .newBuilder()
                .GET()
                .header("accept", "application/json")
                .uri(URI.create(URL))
                .build();

        HttpResponse<String> httpResponses = httpClient.send(httpRequest, HttpResponse.BodyHandlers.ofString());
        //System.out.println(httpResponses.body()); // stampa l'intero file JSON

        // Parsing JSON into Objects
        ObjectMapper objectMapper = new ObjectMapper();
        List<Pet> pets = objectMapper.readValue(httpResponses.body(), new TypeReference<List<Pet>>() {
        });
        //pets.forEach(System.out::println); oppure
        for (Pet pet : pets) {
            System.out.println(pet.getId() + ", " + pet.getType() + ", " + pet.getPrice());
        }
    }

    public static void jsonPostRequest() throws IOException, InterruptedException {
        final String URL = "https://vbzrei5wpf.execute-api.us-east-1.amazonaws.com/test/pets";
            final Map<String, Object> values = new HashMap<>();
        values.put("type", "octopus");
        values.put("price", 12.99);

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

        HttpClient httpClient = HttpClient.newHttpClient();
        HttpRequest request = HttpRequest
                .newBuilder()
                .uri(URI.create(URL))
                .POST(HttpRequest.BodyPublishers.ofString(requestBody))
                .build();

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

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

    public static void jsonDeleteRequest() {
        final String URL = "https://vbzrei5wpf.execute-api.us-east-1.amazonaws.com/test/pets";
        // TODO...
    }
}

A DELETE request is sent the same way as GET , just use the "DELETE" method instead of "GET":

    HttpRequest httpRequest = HttpRequest
            .newBuilder()
            .DELETE()
            .uri(URI.create(URL))
            .build();

If you want to delete an object use @Joni's answer. If you want to specify the id of the object, add /<id> to your url

https://vbzrei5wpf.execute-api.us-east-1.amazonaws.com/test/pets/1 would be the url to delete the element with the id 1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM