CURL HTTP/1.1 415 Unsupported Media Type Error


We often get HTTP/1.1 415 status code for server error Unsupported Media Type. This error occurs when any HTTP client sends the wrong Content-Type.

When using CURL or any other HTTP client tool to invoke REST API with POST or PUT, the default content type is application/x-www-form-urlencoded or multipart/form-data which is wrong if the server is expecting another content type like application/json or something else.

For the example here is Spring Rest Controller method

    @ApiOperation(value = "Add new email and company for 2F authentication",response = TotpResource.class)
    @PostMapping(consumes = "application/json", produces = "application/json")
    public ResponseEntity<TotpResource> addNew(@Valid @RequestBody TotpResource totpResource) {
        totpResource = this.totpService.addNew(totpResource);

        return ResponseEntity.created(URI.create("/" + totpResource.getEmailId())).body(totpResource);
    }

In this case, the expected correct headers would be

Accept: application/json"
Content-Type: application/json

For calling the above API we need to use the following CURL command

curl --header "Content-Type: application/json" \
  --header "Accept: application/json" \
  --request POST \
  --data '{...}' \
  http://localhost:8080/api/totp

You should check CURL options documentation for more details curl man page (mit.edu)


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.