Ethan_J
Ethan_J

Reputation: 65

Adding extra server call on Springboot application causing issue with CORS

For context, I have been working on a springboot application with a ReactJS frontend. The ReactJS frontend connects to the springboot application, and the application makes calls to another server to get the requested data to display on the front end. I had gotten to a point where I was able to make a post request with a JSON payload, get the requested data in the form of another JSON payload, and display it to the frontend with no issues.

I had to implement a change, and add one more variable to the object that was being returned to the front end. And now the browser is telling me that:

Access to fetch at [url] been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

All that is different is the addition of the data to the object being returned, and the fact I am making another call to the server where the data is stored.

Here is the Response header when I dont make the additional call, and I get back the JSON payload

HTTP/1.1 200
Vary: Origin
Vary: Access-Control-Request-Method
Vary: Access-Control-Request-Headers
Access-Control-Allow-Origin: *
Set-Cookie: [I erased the cookie after pasting]
Content-Type: text/plain;charset=ISO-8859-1
Content-Length: 83363
Date: Thu, 09 Jul 2020 18:05:40 GMT
Keep-Alive: timeout=60

Here is the Response Header when I make the additional call

HTTP/1.1 500
Connection: close
Date: Thu, 09 Jul 2020 18:13:24 GMT

Ive debugged the backend, and no errors are thrown on the server side, and nothing happens while processing the data. I have all of a sudden just started getting the issue with CORS.

The following is my post request that has been working so far

fetch(url, {
            headers: {
                'Content-Type': 'text/plain',
                'Accept': '*/*',
                'Accept-Encoding': 'gzip, deflate, br',
            },
            method: 'post',
            body: JSON.stringify(jsonData)
        })

Im sure I have enabled everything I had to do on the Springboot side for CORS considering It works all the time except for when trying to get the extra data. If there is something I am missing I would like to know, and if this issue can be clarified by reading documentation on springboot and cors please point me in that direction.

Note: I have also tried adding

@Configuration
@EnableWebMvc
public class SpringBootConfiguration implements WebMvcConfigurer{

    @Override
    public void addCorsMappings(CorsRegistry registry) {
            registry.addMapping("/**");
    }
}

to my application, but that does not seem to work either. This is all still fairly new to me, so I am unsure what is going wrong.

Thank you.

Upvotes: 1

Views: 194

Answers (2)

Ethan_J
Ethan_J

Reputation: 65

The header was too large now with the added data it seems. So all I had to do was increase the max-http-header-size in my application.properties. I can now get the full JSON payload with zero issues. I was too focused on the error on the frontend side that I neglected to go deep enough into the backend to see what was happening.

I added in my application.properties

server.max-http-header-size = 48000

I didnt actually set it to 48000, its much lower than that, I found the answer here

How to set max-http-header-size in spring boot 2.x application

Upvotes: 1

Dylan
Dylan

Reputation: 2219

I had issues with CORS andthe snippet you gave was something I had tried but it ended up breaking some other endpoints for unknown reasons. You can add @CrossOrigin(origins = "URL", allowedHeaders = "*") to your controllers.

Such as

@Controller
@CrossOrigin(origins = "localhost", allowedHeaders = "*")
public class Controller

Upvotes: 0

Related Questions