c#httpresponse返回状态代码406不可接受
我创建了一个连接到API的Azure函数。这是代码:
public static async Task<HttpResponse> RunProduction(
[HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
ILogger log)
{
_log = log;
API_URL = @"{API_URL}";
BEARER_TOKEN = Environment.GetEnvironmentVariable("{ENV_NAME}");
if (BEARER_TOKEN.Equals("")) throw new Exception("Bearer token could not be retrieved");
var response = req.HttpContext.Response;
response.Clear();
response.StatusCode = 200;
response.ContentType = "text/plain";
//This is where the API will be called.
//The method is not changing anything in the response (yet)
response = await ExecuteMethods(req, response);
return response;
}
我正在使用Postman在本地测试该功能。在Postman中,我有以下标题:
每次测试该功能时,我都会收到一个不可接受的406。当我评论方法executeMethods()
的行时,这种情况甚至会发生。 有什么想法如何摆脱406-Error并再次获得200 statuscode?
I have created an Azure Function which connects to an API. This is the code:
public static async Task<HttpResponse> RunProduction(
[HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
ILogger log)
{
_log = log;
API_URL = @"{API_URL}";
BEARER_TOKEN = Environment.GetEnvironmentVariable("{ENV_NAME}");
if (BEARER_TOKEN.Equals("")) throw new Exception("Bearer token could not be retrieved");
var response = req.HttpContext.Response;
response.Clear();
response.StatusCode = 200;
response.ContentType = "text/plain";
//This is where the API will be called.
//The method is not changing anything in the response (yet)
response = await ExecuteMethods(req, response);
return response;
}
I am testing the Function locally with Postman. In Postman, I have the following headers:
Every time I test the Function, I receive a 406 Not Acceptable. This even happens when I comment the line where the method ExecuteMethods()
is being called.
Any ideas how I can get rid of the 406-error and get my 200-statuscode again?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是一个老人,但是我最近也有类似的问题,并且能够通过创建 textInputformatter 。如果相关,我的项目是使用隔离工人模型的Net8.0。如果您使用的是带有隔离工人模型的旧dotnet版本,则以下信息可能不准确。
我的代码使用jsonresult从请求返回iActionResult。我添加了一个端点,该端点返回了TSV字符串,但是当我使用OkoBjectResult时,我的响应内容类型以“ text/plain; carset; carset = utf-8”返回,并且得到了406响应。
挖掘后,我决定尝试制作自己的textInputformatter,并发现此示例可以从:
https://github.com/aspnet/entropy/blob/93d29fb35039c2dc71a998fc76222222f4f4f4f4f7e72/服务的初始配置,例如
我还需要明确指定OkoBjectResult的内容类型是“文本/平原”,以使此格式化启动。
This is an oldie, but I had a similar issue recently and was able to resolve it by creating a TextInputFormatter. My project is net8.0 using the isolated-worker model, if it's relevant. If you're using an older dotnet version w/o the isolated-worker model, the following info might not be accurate.
My code used JsonResult to return the IActionResult from the request. I added an endpoint that returned a TSV string instead, but when I used OkObjectResult my response content type came back as "text/plain; carset=UTF-8" and I got the 406 response.
After some digging, I decided to try making my own TextInputFormatter and I found this example to work from: https://github.com/aspnet/Entropy/blob/93d29fb35039c2dc7e71a998fc7ff7622f4f7e72/samples/Mvc.Formatters/TextPlainInputFormatter.cs
This needs to added to the service's InputFormatters when you do your initial configuration of the service like this.
I also needed to explicitly specify that the OkObjectResult's content type is "text/plain" to get this formatter to kick in.