Fix: Spring Boot REST HTTP Status 415 - Unsupported Media Type Error


If you are trying to perform a POST/DELETE API call using Spring Boot REST (@RestController) and you get HTTP 415 error, well that's because you need to provide the header type as,

Content-Type: application/json

If you do not provide a Content-Type header, by default the Request will be considered as plain text and you would also see a warning in your application logs.

WARN 4189 --- [nio-8080-exec-8] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.web.HttpMediaTypeNotSupportedException: Content-Type 'text/plain;charset=UTF-8' is not supported]


Fix:

cURL Example:

curl --location 'http://localhost:8080/update-user' \
--header 'Content-Type: application/json' \
--data '{
    "userId":10,
    "userName":"Ron"
}'

If you are using Postman, make sure to pass in Content-Type as application/json under the Headers tab.

Adding Content-Type in Postman

Facing issues? Have Questions? Post them here! I am happy to answer!

Author Info:

Rakesh (He/Him) has over 14+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.

Follow him on: X

You can also reach out to him via e-mail: rakesh@code2care.org

Copyright © Code2care 2024 | Privacy Policy | About Us | Contact Us | Sitemap