javaspring-boothttp-redirecthttpsrailway

Sring Boot. Failed to fetch. Possible Reasons: CORS Network Failure URL scheme must be "http" or "https" for CORS request


when i try swagger on my local host the app works correctly(the api works), but i have done a deploy on railway and when i try to test an API from the swagger i get this error:

Failed to fetch. Possible Reasons: CORS Network Failure URL scheme must be "http" or "https" for CORS request.

I think it occur cause in the local app i do a request with a http link but in deploy version on railway it automaticaly was set to https, can this cause the erorr ?

I try to add to my application a WebConfig :

@Configuration
public class WebConfig implements WebMvcConfigurer {
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("*")
                .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
                .allowedHeaders("*");
    }
}

But this didn't help i still get the same error

!!!!! When i make a request from the postman i get a right response with this link(this is a get request): https://exciting-wonder-production.up.railway.app/country, but from the swagger i get the error :
Failed to fetch. Possible Reasons: CORS Network Failure URL scheme must be "http" or "https" for CORS request.


Solution

  • try update swagger configuration class and swager ui to use the correct https url for api:

    @Configuration
    public class SwaggerConfig {
    @Bean
    public OpenAPI customOpenAPI() {
        return new OpenAPI()
                .servers(List.of(new Server().url("https://exciting-wonder-production.up.railway.app")));
    }
    }
    

    replace .allowedOrigins("*") with your deployed url .allowedOrigins("https://exciting-wonder-production.up.railway.app")

    @Configuration
    public class WebConfig implements WebMvcConfigurer {
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("https://exciting-wonder-production.up.railway.app")
                .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
                .allowedHeaders("*")
                .allowCredentials(true);
    }
    }