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

Tim*_*meh 7 c# asp.net-core asp.net-core-2.1

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. 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."

The action looks like this:

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

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:

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

使用者使用的(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
));
Run Code Online (Sandbox Code Playgroud)

删除该"Content-Type: application/json",行会将请求转换为有效请求,因此我们有99.9%的把握确保此标头的添加是邪恶的。

Nko*_*osi 3

考虑在管道早期删除中间件中的标头。

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();
    });

    //...
}
Run Code Online (Sandbox Code Playgroud)