我正在使用flurl提交HTTP请求,这非常有用。现在,我需要改变“的Content-Type”头对于一些要求的“应用/ JSON;的OData =详细”如何更改FLURL客户端的HTTP请求内容类型?
public async Task<Job> AddJob()
{
var flurlClient = GetBaseUrlForGetOperations("Jobs").WithHeader("Content-Type", "application/json;odata=verbose");
return await flurlClient.PostJsonAsync(new
{
//Some parameters here which are not the problem since tested with Postman
}).ReceiveJson<Job>();
}
private IFlurlClient GetBaseUrlForOperations(string resource)
{
var url = _azureApiUrl
.AppendPathSegment("api")
.AppendPathSegment(resource)
.WithOAuthBearerToken(AzureAuthentication.AccessToken)
.WithHeader("x-ms-version", "2.11")
.WithHeader("Accept", "application/json");
return url;
}
你可以看到我是如何尝试添加上述标题( .WithHeader("Content-Type", "application/json;odata=verbose")
)
不幸的是这给了我以下错误:
"InvalidOperationException: Misused header name. Make sure request headers are used with HttpRequestMessage, response headers with HttpResponseMessage, and content headers with HttpContent objects."
我也试过flurl的“ConfigureHttpClient”方法,但怎么也找不到/在哪里设置conten t型标题。
的可能的复制[如何内容头部添加到Flurl(https://stackoverflow.com/questions/32829763/how-to- add-content-header-to-flurl) –
你需要创建一个'HttpRequestMessage'添加一个内容并指定它的类型 – NtFreX
这是2.0版本中修复的小故障(https://www.nuget.org/packages/ Flurl.Http /)。 –