Spring Boot + React.js Axios: 403 Forbidden for HTTP POST calls
In my dev environment I have a BE built using Spring Boot and an FE built using React.js (Redux + Axios). Whenever I try to do an HTTP POST with Axios, I disallow 403. The HTTP GET works as expected, and the HTTP POST calls made from Postman also work fine .
PersonController.java
@RestController
@RequestMapping(value = "/api/v1/person")
public class PersonController {
@Autowired
private PersonRepository mPersonDAO;
@PostMapping
public ResponseEntity<Person> create(@RequestBody Person person) {
Person result = mPersonDAO.save(person);
return new ResponseEntity<>(result, HttpStatus.CREATED);
}
}
React.js service
import axios from 'axios';
const BASE_URL = 'https://localhost:8080/api/v1';
const PERSON_API = BASE_URL + '/person'
export function syncUser (person, onSuccess) {
axios({
method: 'post',
url: PERSON_API,
data: {
person
}
}).then((response) => {
onSuccess(response);
}).catch(function (error) {
console.log(error);
});
}
StackTrace:
localhost/:1 XMLHttpRequest cannot load http://localhost:8080/api/v1/person.
dispatchXhrRequest @ xhr.js:177
xhrAdapter @ xhr.js:12
dispatchRequest @ dispatchRequest.js:52
Response for preflight has invalid HTTP status code 403
+3
source to share