Making an http request using axios to a spring backend and getting the following unexpected error.
Resolved [org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/json;charset=UTF-8' not supported]
Isnt a 415 when the Accept
header of the server does not match the Content-Type
header of the client?
Response Headers
HTTP/1.1 415
Vary: Origin
Vary: Access-Control-Request-Method
Vary: Access-Control-Request-Headers
Access-Control-Allow-Origin: *
Accept: application/json, application/*+json
Content-Type: application/json
Transfer-Encoding: chunked
Date: Wed, 20 Apr 2022 05:28:54 GMT
Keep-Alive: timeout=60
Connection: keep-alive
Request Headers
POST /posts HTTP/1.1
Host: 192.168.1.49:8080
User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:99.0) Gecko/20100101 Firefox/99.0
Accept: application/json, text/plain, */*
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Content-Type: application/json
Content-Length: 86
Origin: http://localhost:3000
Connection: keep-alive
Referer: http://localhost:3000/
Sec-GPC: 1
Since they match im not really sure why it would be considered a 415.
const client = axios.create();
client.defaults.headers["Content-Type"] = "application/json"
client.defaults.headers["Access-Control-Allow-Origin"] = "*/*";
export async function savePost(post) {
return client.post(constants.backend_url + "/posts", post);
}
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.6.6</version>
</dependency>
@PostMapping
public ResponseEntity<Post> createNewPost(@RequestBody Post post) {
...
}