我正在使用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 /)。 –