简体   繁体   English

如何接受 JSON 作为其内容类型的无主体 GET 请求?

[英]How to accept a bodyless GET request with JSON as it's content type?

After migrating to ASP.NET Core 2.1 we have realized that some consumers of our API are sending GET requests with the Content-Type header set to application/json .在迁移到 ASP.NET Core 2.1 后,我们意识到我们的 API 的一些消费者正在发送Content-Type header 设置为application/json的 GET 请求。 Sadly, these requests have not been rejected in the past (even though they should have), nevertheless this still is a breaking change..可悲的是,这些请求过去没有被拒绝(即使他们应该拒绝),但这仍然是一个突破性的变化。

Since our consumers need to fix this issue on their end, and this will take some time, we would like to temporarily accept these requests so we're not stuck waiting for this.由于我们的消费者需要自己解决这个问题,这需要一些时间,我们希望暂时接受这些请求,这样我们就不会等待这个问题。

The framework (correctly) rejects the request with the following error message: "A non-empty request body is required."框架(正确)拒绝请求并显示以下错误消息: "A non-empty request body is required."

The action looks like this:动作如下所示:

[Route("api/file/{id:guid}")]
public async Task<IActionResult> Get(Guid id)
{
     // Some simple code here
}

The code inside the action isn't being reached as the error is already been thrown before it reaches the action (due to the incorrect request).未到达操作内的代码,因为错误在到达操作之前已被抛出(由于请求不正确)。

@Nkosi's solution resulted in the same response: @Nkosi 的解决方案产生了相同的响应:

[HttpGet("api/file/{id:guid}")]
public async Task<IActionResult> Get([FromRoute]Guid id)
{
     // Some simple code here
}

The (PHP) cURL that the consumer uses is this:消费者使用的 (PHP) cURL 是这样的:

$ch = curl_init(self::API_URL."/file/".$id);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FRESH_CONNECT, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    "Content-Type: application/json",
    "Application: APPKey ".$this->AppKey,
    "Authorization: APIKey ".$this->ApiKey
));

Removing the "Content-Type: application/json", line turns the request into a valid requests, so we're 99.9% sure that the addition of this header is the evildoer.删除"Content-Type: application/json",行会将请求变为有效请求,因此我们有 99.9% 的把握确定添加此 header 是作恶者。

Consider removing the header in a middleware early in the pipeline.考虑在管道的早期删除中间件中的 header。

public void Configure(IApplicationBuilder app) {
    app.Use(async (context, next) => {
        var request = context.Request;
        var method = request.Method;
        IHeaderDictionary headers = request.Headers;
        string key = "Content-Type";
        if (method == "GET" && request.Headers.ContainsKey(key)) {
            headers.Remove(key);
        }

        // Call the next delegate/middleware in the pipeline
        await next();
    });

    //...
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何获取 HTTP Post 请求的 JSON 内容? - How to get the JSON content of a HTTP Post request? 如何在运行时确定ServiceStack请求中的接受类型 - How to determine at runtime the accept type in a ServiceStack request 如何将Content-Type标头添加到.Net GET Web请求? - How to add the Content-Type header to a .Net GET web request? Owin / WebApi强制仅接受application / json Accept和Content-Type标头 - Owin/WebApi force to to accept only application/json Accept and Content-Type headers 如果Content-Type为“ text / plain”,则GET RestSharp请求返回空值,而如果Content-Type为“ application / json”,则返回数据 - GET RestSharp request returns null value if the Content-Type is “text/plain”, whereas if Content-Type is “application/json” data is returned 我正在尝试从HttpRequest消息中检索输入请求。 HttpRequest中的内容显示了内容类型和长度。 如何获取json? - I am trying to retrieve input request from HttpRequest message. Content in HttpRequest showing content type and length. How do I get json out? 如何在不加载完整内容的情况下获取请求的响应类型 - How to get response type of the request without loading the full content 抽象类中无体抽象方法有什么问题? - What's wrong with bodyless abstract methods in abstract class? 将 Content-Type 添加到 Swashbuckle Get 请求 - Adding the Content-Type to the Swashbuckle Get Request (UWP)如何在GET请求中添加不带Content-Length的Content-Type标头 - (UWP) How to add Content-Type header without Content-Length in GET request
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM