我使用ocelot作为网关,获取请求工作得很好,但后请求返回400个错误请求
Bad Request
但同样的请求在Postman上运行,并返回200,如图所示,
Success
我们可以注意到swagger和postman都指向同一个url,所以问题不是ocelot.json文件。这是我的Ocelot.json文件
{
"Routes": [
{
"DownstreamPathTemplate": "/api/{everything}",
"DownstreamScheme": "https",
"DownstreamHostAndPorts": [
{
"Host": "localhost",
"Port": 44340
}
],
"UpstreamPathTemplate": "/gateway/v1/starter/{everything}",
"UpstreamHttpMethod": [ "Get", "Post", "Put", "Delete" ],
"SwaggerKey": "starter"
},
{
"DownstreamPathTemplate": "/api/{everything}",
"DownstreamScheme": "http",
"DownstreamHostAndPorts": [
{
"Host": "localhost",
"Port": 21021
}
],
"UpstreamPathTemplate": "/gateway/v1/abpstarter/{everything}",
"UpstreamHttpMethod": [ "Get", "Post", "Put", "Delete" ],
"SwaggerKey": "abpstarter"
}
],
"SwaggerEndPoints": [
{
"Key": "starter",
"Config": [
{
"Name": "starter API",
"Version": "v1",
"Url": "https://localhost:44340/swagger/v1/swagger.json"
}
]
},
{
"Key": "abpstarter",
"Config": [
{
"Name": "abpstarter API",
"Version": "v1",
"Url": "http://localhost:21021/swagger/v1/swagger.json"
}
]
}
],
"GlobalConfiguration": {
"BaseUrl": "http://localhost:3000",
"RequestIdKey": "OcRequestId"
}
}
2条答案
按热度按时间yxyvkwin1#
通过Swagger发出请求时遇到的HTTP 400错误可能有几种可能的原因。由于请求在Postman中正常工作,因此问题可能与Ocelot配置或Swagger如何与其交互有关。请记住,Postman和Swagger之间的行为差异可能是由于各种原因造成的,包括头、身份验证、CORS和其他特定于应用程序的设置。你能和我们分享一下虎猫的 Swagger 的配置吗?我建议你重新检查一下swagger的配置。
dpiehjr42#
当我在下游服务的启动文件中注解这个“AddControllersWithViews”时,它工作得很好,因为它可以防止跨站点请求伪造(XSRF/CSRF)攻击