On my dev environment I have a BE built using Spring Boot and a FE built using React.js (Redux + Axios). Whenever I try to perform an HTTP POST using Axios I get 403 forbidden. HTTP GET calls work as expected and HTTP POST calls made from Postman work fine as well.
@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);
}
}
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);
});
}
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
Found the problem! It was actually a CORS issue. I had to enable CORS on SB from the domain the request was comming from. The answer was posted here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With