I'm "Getting 'Http failure response for http://localhost:8080/api/user/auth/login: 0 Unknown Error' when sending a POST request to my Spring Boot API Gateway from my Angular app. CORS is configured correctly, and the same request works fine when sent directly to the user-service. The API Gateway logs don't show any errors, and the browser console doesn't provide additional insights. Any suggestions on how to troubleshoot this issue?".
API Gateway application.properties
eureka.client.register-with-eureka=true
eureka.client.fetch-registry=true
spring.application.name=api-gateway
eureka.client.service-url.defaultZone= http://localhost:8761/eureka
server.port=8080
## LOGS
logging.level.root=INFO
#class responsible for defining and identifying routes
logging.level.org.springframework.cloud.gateway.route.RouteDefinitionLocator = INFO
logging.level.org.springframework.cloud.gateway = TRACE
## END LOGS
## Routes
spring.cloud.gateway.routes[0].id=user-service
spring.cloud.gateway.routes[0].uri = lb://user-service
spring.cloud.gateway.routes[0].predicates[0]=Path=/api/user/**
API GatewayCors config
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class CorsConfig implements WebMvcConfigurer {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedOrigins("*") // Replace with your Angular application's URL
.allowedMethods("GET", "POST", "PUT", "DELETE")
.allowedHeaders("Content-Type", "Authorization");
}
}
PS: When I change the URL in my angular app to directly send a request to the user-service everything works fine.
please create an application.yml file in your api-gateway and place the following configuration inside.
spring:
application:
name: gateway
cloud:
gateway:
global-cors:
corsConfigurations:
'[/**]':
allowedOrigins: "*"
allowedMethods:
- GET
- POST
- PUT
- DELETE
- PATCH
allowedHeaders:
- "Origin"
- "Content-Type"
- "Accept"
- "Authorization"
- "User-Key"
- "Request-Tracker"
- "Session-Tracker"
- "X-XSRF-TOKEN"
- "X-IBM-CLIENT-ID"
- "Message-ID"
- "X-IBM-CLIENT-SECRET"
default-filters:
- DedupeResponseHeader=Access-Control-Allow-Credentials Access-Control-Allow-Origin, RETAIN_FIRST