4

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
Humble Student
  • 3,755
  • 4
  • 20
  • 35

1 Answers1

1

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.

Community
  • 1
  • 1
Humble Student
  • 3,755
  • 4
  • 20
  • 35