# Difference Between Put and Patch Requests in Spring Boot

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.

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 align="center")

Let's create a method that will update an existing customer in our controller.

```java
    @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 align="center")

And the request is going to update the database:

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

## 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.

```java
    @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 align="center")

The new customer appears into our database:

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

Now let's perform a PATCH request:

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

The phone number has now changed.

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

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, please consider subscribing to my newsletter. 😊

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/)
