Dog
Dog

Reputation: 2906

Axios get request from React to Spring Boot application returns Network Error

I have a SpringBoot application with a @Controller that accepts a getRequest to localhost:8080/all and returns JSON. I have entered this domain in the browser and see the returned JSON.

However, when I attempt to hit this url with an Axios get request from my React app (which is running locally), rather than returning the JSON data, it returns the following error:

Network Error at createError (http://localhost:3000/static/js/bundle.js:1634:15) at XMLHttpRequest.handleError (http://localhost:3000/static/js/bundle.js:1170:14)

I checked Chrome's network tab for the get request and it doesn't return anything for the preview or response, but does indicate:

Request URL: https://localhost:8080/all
Referrer Policy: no-referrer-when-downgrade

in the Headers section.

Both applications are running locally and when I try a different axios get request (for a different API) it returns the JSON successfully. How can I successfully return JSON from my Spring Boot application?

The React setup for the API call is:

  componentDidMount() {
    axios.get('https://localhost:8080/all')
    .then(function (response) {

      console.log(response);
    })
    .catch(function (error) {

      console.log(error);
    });
  }

And the controller action in the Spring Boot app is:

@RestController    
public class UserController {

    @Autowired
    UserJDBCDao dao;

    @GetMapping(path="/all")
    public @ResponseBody Iterable<User> getAllUsers() {
        // This returns a JSON or XML with the users
        return dao.findAll();
    }


}

Upvotes: 2

Views: 2700

Answers (1)

Dog
Dog

Reputation: 2906

After checking the Spring Boot error log it indicated:

java.lang.IllegalArgumentException: Invalid character found in method name. HTTP method names must be tokens

So I changed https to http and the request worked. I'm not sure if this is the most secure way, so someone else might have a better solution.

Upvotes: 1

Related Questions