Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Boot + React.js Axios: 403 Forbidden for HTTP POST calls

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.

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
like image 959
Humble Student Avatar asked Apr 18 '26 12:04

Humble Student


1 Answers

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.

like image 125
Humble Student Avatar answered Apr 21 '26 03:04

Humble Student