在API调用中获取方法以保留标头
我有一个asp.net核心应用程序,我希望在响应中保留请求头。 我在《邮递员》中有3个标题在API调用中获取方法以保留标头,api,asp.net-core,response-headers,Api,Asp.net Core,Response Headers,我有一个asp.net核心应用程序,我希望在响应中保留请求头。 我在《邮递员》中有3个标题 Authorization: "Bearer xxxxx" azet-Accept: application/json" azet-UseLatestversion: true 这是我在配置方法中设置的内容 app.Use( async (context, next) => { context.Response.
Authorization: "Bearer xxxxx"
azet-Accept: application/json"
azet-UseLatestversion: true
这是我在配置方法中设置的内容
app.Use(
async (context, next) =>
{
context.Response.Headers.Add("X-Content-Type-Options", "nosniff");
context.Response.Headers.Add("Content-Security-Policy", "default-src 'self'");
context.Response.Headers.Add("Referrer-Policy", "no-referrer");
context.Response.Headers.Add(
"Feature-Policy",
"accelerometer 'none'; camera 'none'; geolocation 'none'; gyroscope 'none'; magnetometer 'none'; microphone 'none'; payment 'none'; usb 'none'");
context.Response.Headers.Add("X-Frame-Options", "DENY");
await next();
});
app.UseHttpsRedirection();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(
endpoints =>
{
endpoints.MapControllers();
});
我希望在响应中返回像azet UseLatestversion这样的标题
有什么想法吗
谢谢
[HttpGet]
[Route("{apiPath}/{*requestUri}")]
public async Task<IActionResult> GetAsync(string apiPath, string requestUri = "")
{
var queryString = this.Request.QueryString.Value;
var headers = this.Request
.Headers
.ToDictionary(x => x.Key, y => y.Value.FirstOrDefault());
var response = await this.proxy.GetAsync(
apiPath, requestUri, queryString, headers, this.GetUserScopes());
this.logger.LogInformation(
$"GET statement received from {apiPath} {requestUri} {queryString} {DateTime.Now}.");
var test = response.Content;
if (response.HttpStatus != HttpStatusCode.OK)
{
this.logger.LogError($"{apiPath} {requestUri} {queryString} {response.HttpStatus}: {response.Content}");
return this.StatusCode((int)response.HttpStatus, ErrorResponse.GetDefaultMessageForStatusCode((int)response.HttpStatus));
}
return this.Content(response.Content?.ToString(), response.ContentType);
}
[HttpGet]
[路由(“{apiPath}/{*requestUri}”)]
公共异步任务GetAsync(字符串apiPath,字符串requestUri=”“)
{
var queryString=this.Request.queryString.Value;
var headers=this.Request
.标题
.ToDictionary(x=>x.Key,y=>y.Value.FirstOrDefault());
var response=等待this.proxy.GetAsync(
apiPath、requestUri、queryString、headers、this.GetUserScopes());
此为.logger.LogInformation(
$“从{apiPath}{requestUri}{queryString}{DateTime.Now}接收的GET语句”;
var测试=响应。内容;
if(response.HttpStatus!=HttpStatusCode.OK)
{
LogError($“{apiPath}{requestUri}{queryString}{response.HttpStatus}:{response.Content}”);
返回此.StatusCode((int)response.HttpStatus,ErrorResponse.GetDefaultMessageForStatusCode((int)response.HttpStatus));
}
返回此.Content(response.Content?.ToString(),response.ContentType);
}
我希望在响应中保留请求头
下面是一个工作演示:
app.Use(
async (context, next) =>
{
foreach (var header in context.Request.Headers)
{
if(!header.Key.Contains(":"))
{
context.Response.Headers.Add(header.Key, header.Value.ToString());
}
}
await next();
});
结果:
不确定您的需求是什么,因为您在中间件中所做的工作可以很好地在响应中返回自定义标头。是否要自动获取请求标头并将其保存到响应中?这很有效。谢谢然而,我的响应添加了一些自定义标题,但它们没有显示在postman中。这是我的返回语句,“returnthis.Content(response.Content?.ToString(),response.ContentType);”如何在postman中显示自定义标题?您的
响应是什么?
?是否可以共享更多代码?似乎您调用了另一个api并获得了响应?