[Solved]-"Spring Boot, typeerror: failed to execute 'fetch' on 'window': request with get/head method cannot have body."


"Swagger TypeError: Failed to execute 'fetch' on 'Window': Request with GET/HEAD method cannot have body"

To solve the "Swagger TypeError: Failed to execute 'fetch' on 'Window': Request with GET/HEAD method cannot have body in Spring Boot" error, you need to ensure that your Swagger configuration is correct and that you're not sending a body with a GET or HEAD request.

  1. Check Swagger Configuration: First, make sure your Swagger configuration is correct. Ensure that you're using the appropriate annotations and settings to define your API endpoints and operations.
  2. Review Request Methods: Double-check your Swagger documentation to verify that you're using the correct HTTP request methods for your API endpoints. GET and HEAD requests typically do not include a request body.
  3. Inspect Swagger UI Code: If you're using Swagger UI to interact with your API, inspect the code generated by Swagger UI to see if there are any inconsistencies or errors in the request definitions.
  4. Debug Code Sending Requests: Review the code responsible for sending requests to your Spring Boot application. Ensure that you're not mistakenly including a request body in GET or HEAD requests.
  5. Validate API Requests: Before sending requests, validate them against your Swagger documentation to ensure they conform to the expected format and method.
  6. Update Swagger Annotations: If necessary, update your Swagger annotations or configuration to explicitly specify that GET and HEAD requests should not include a request body.
  7. Test Endpoints: Test your API endpoints using tools like Postman or curl to ensure that they behave as expected and do not encounter the TypeError.

Here's how I resolved the issue:

I initially had a method defined like this:

        
            [HttpGet]
            public IEnumerable Get(MyObject dto)
            {
                // Code implementation...
            }
        
    

And I encountered an error. I suspected that Swagger UI was interpreting the parameters of the HTTP GET request as coming from the request body (FromBody), causing it to use the curl -d flag.

To fix this, I added the [FromQuery] decorator to the parameter, indicating that it should be taken from the query string:

        
            [HttpGet]
            public IEnumerable Get([FromQuery]MyObject dto)
            {
                // Code implementation...
            }
        
    

After making this change, the problem was resolved, and Swagger UI correctly interpreted the parameters, resolving the issue with the curl -d flag.

To avoid encountering this error, ensure that you annotate parameters in your controller with @RequestParam, like so:

        
            @GetMapping("/get")
            public Response getData(@RequestParam String param) {
                // Code implementation...
            }
        
    

By using @RequestParam, you explicitly specify that the parameter should be obtained from the request parameters, helping to prevent any issues with parameter interpretation.

This error occurs due to an incorrect argument type. Simply change [FromBody] to [FromQuery].