0

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...
    }
}
George
  • 2,820
  • 4
  • 29
  • 56
  • I think that this post will help you : https://stackoverflow.com/questions/43241436/java-http-delete-with-request-body – planben Aug 24 '20 at 12:04

2 Answers2

0

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();
Joni
  • 108,737
  • 14
  • 143
  • 193
  • How can I specify the id of the object I want to delete? –  Aug 24 '20 at 12:09
  • 1
    The same way you specify the id of the object with the other request methods: in the path or in a query parameter. – Joni Aug 24 '20 at 12:11
0

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

Yochyo
  • 72
  • 3
  • 5