Search code examples
javaspringspring-bootpostmanhttp-method

PATCH method with enum value doesn't work with Postman but works with browser


Here is my Spring Controller with the @PatchMapping :

@PatchMapping("/mode/{numClient}")
public Response<Object> mode(
       @PathVariable Integer numClient,
       @RequestBody ModeEnum mode,
       @RequestHeader(name = "Authorization") String bearer
    ) throws BusinessException, NoSuchElementException {
    // Code here
}

With my ModeEnum that looks like this :

public enum ModeEnum {
    @JsonProperty("A")
    AUTOMATIQUE('A'),
    @JsonProperty("M")
    MANUEL('M');

    private final Character mode;

    private ModeEnum(Character mode) {
        this.mode = mode;
    }

    public Character getMode() {
        return this.mode;
    }
}

I use Angular as my frontend framework and this line of code

this.http.patch(`${url}/${numClient}`, {mode})

send this within the body :

{
    "mode": "M"
}

and it works !

Now, when I try to send the same thing with Postman (or the code Postman generate), it doesn't work, I have a 400 bad request :

Request : Postman request

Headers : Postman headers

Response : Postman response

What I tried :

  • use an encapsulated object instead of using directly my Enum in both Angular and Java/Spring
  • send an OPTIONS request first before trying my PATCH in Postman
  • only send "A" or "M" within the body of the Postman request

But none of them worked so I'm running out of ideas :(


Solution

  • You're not sending the Authorization RequestHeader from postman. Given that you've defined this in your handler, you either need to add required = false, or pass the Authorization header, otherwise you'll get a 400.