What's the difference between a PUT and PATCH request in Spring Boot?
Published: 2022-01-10 10:08:30

Have you ever wondered the exact difference between a PUT and a PATCH HTTP request? 

In this article, I will provide you with some examples to show you the difference between a PUT and a PATCH request.

If you haven't read my previous article about " [How to Create a Spring Boot REST API](https://techwithmaddy.com/how-to-create-a-spring-boot-rest-api)", please take a look at that as this article will operate as an extension.

Let's start.

**What is a PUT request?**

A PUT request is a request that *updates an existing record*. 

This is what we have in our database:

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1641728756971/m0aqk-HRxW.png)

In our controller, let's create a method that will update an existing customer.

```
    @RequestMapping(method = {PUT}, path = "/update", produces = MediaType.APPLICATION_JSON_VALUE)
    public Customer updateExistingCustomer(@RequestBody Customer customer) {
        return customerService.saveCustomer(customer);
    }
```

Let's change the customer with id 16 (Steve Austin) with the name "Patrick David".

The Postman request will look like this:


![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1641732865969/VfynTAS8D.png)

And the request is going to update the database:

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1641732919677/xDGfORKop.png)

**What is a PATCH request?**

A PATCH request is an HTTP request that *performs partial updates*. 

Let's see an example.

Let's add the method to perform the PATCH request in our controller.

```
    @RequestMapping(method = {PATCH}, path = "/update/{id}/{phoneNumber}", produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<Customer> updatePartialCustomer(@PathVariable Integer id, @PathVariable String phoneNumber) {
        try {
            Customer customer = customerRepository.findById(id).get();
            customer.setPhoneNumber(phoneNumber);
            return new ResponseEntity<Customer>(customerService.saveCustomer(customer), HttpStatus.OK);
        } catch (Exception e) {
            return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }
```

The best implementation that I've found to perform an HTTP request recommends using the ResponseEntity that will return the complete HTTP response (status code, body, etc.).

- We search the customer based on their id.
- We update only the phone number.
- We return the new ResponseEntity.
- If something goes wrong in the try, we return an Internal Server Error (= 500 status code).

Let's add a new record to our database.

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1641746618387/MAGwPS3e_.png)


The new customer appears into our database:

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1641746640388/veYvZm2Yl.png)


Now let's perform a PATCH request:

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1641746671411/mt-inHGMf.png)

The phone number has now changed.

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1641746731268/dlpGhozqB.png)

Now you might be wondering:

**What's the difference between a POST, PUT, and PATCH request?**

- A POST request saves new data to the database.

- A PUT request updates an existing record. If you've noticed, we send out the whole body in case of a PUT request.

- A PATCH request updates some parts of an existing record. In case of a PATCH request, we only send the data we want to modify.

Often, I've noticed that engineers use POST and PUT interchangeably. I think we should use a POST request when we want to save new data and PUT only for the updates.

That's it for this article. 

You can find the full Github repository  [here](https://github.com/MaddyGre/CustomerAPI).

If you enjoy my content, check my Hashnode [blogs](https://techwithmaddy.com/). 😊

I hope you've found this helpful.

Until next time! 👋🏾

**ADDITIONAL RESOURCES:**

-  [PatchMapping Spring Boot Example](https://www.sourcecodeexamples.net/2019/10/patchmapping-spring-boot-example.html) 

-  [Using HTTP PATCH in Spring](https://cassiomolin.com/2019/06/10/using-http-patch-in-spring/) 

-  [spring-http-patch-example](https://cassiomolin.com/2019/06/10/using-http-patch-in-spring/) 

**ARTICLES THAT YOU MAY ENJOY READING:**

- "[How to Create a Spring Boot REST API](https://techwithmaddy.com/how-to-create-a-spring-boot-rest-api)"?

- "[Testing with Spring Boot](https://techwithmaddy.com/testing-with-spring-boot)".