gpt4 book ai didi

c# - 调用 Ebay API 时,Content-Language header 会抛出 Misused header name 错误

转载 作者:行者123 更新时间:2023-12-02 20:13:08 26 4
gpt4 key购买 nike

当我使用 Ebay API 向 CreateOrReplaceInventoryItem 发出 PUT 请求时,出现以下错误:

Misused header name. Make sure request headers are used with HttpRequestMessage, response headers with HttpResponseMessage, and content headers with HttpContent objects.

下面的代码会引发错误。

using (var client = new HttpClient())
{
var baseUri = new Uri(string.Format("https://api.sandbox.ebay.com/sell/inventory/v1/inventory_item/{0}", SKU));
client.DefaultRequestHeaders.Add("Authorization", string.Format("Bearer {0}", token));
client.DefaultRequestHeaders.Add("Content-Language", "en-US");

var payload = JsonConvert.SerializeObject(product);
var content = new StringContent(payload, Encoding.UTF8, "application/json");

var response = client.PutAsync(baseUri, content).Result;
return response.Content.ReadAsStringAsync().Result;
}

如果我删除该行:

client.DefaultRequestHeaders.Add("Content-Language", "en-US");

然后我得到一个返回的错误对象:

errors":[{"errorId":25709,"domain":"API_INVENTORY","subdomain":"Selling","category":"REQUEST","message":"Invalid value for header Content-Language."}]} 

我做错了什么?还有另一种我不知道的设置 Content-Language 的方法吗?

最佳答案

由于 Content-Language 是一个内容 header ,因此您应该在 HttpContent 实例中分配它,而不是 DefaultRequestHeaders。根据您的代码将是这样的:

using (var client = new HttpClient())
{
var baseUri = new Uri(string.Format("https://api.sandbox.ebay.com/sell/inventory/v1/inventory_item/{0}", SKU));
client.DefaultRequestHeaders.Add("Authorization", string.Format("Bearer {0}", token));

var payload = JsonConvert.SerializeObject(product);
var content = new StringContent(payload, Encoding.UTF8, "application/json");
content.Headers.Add("Content-Language", "en-US");

var response = client.PutAsync(baseUri, content).Result;
return response.Content.ReadAsStringAsync().Result;
}

关于c# - 调用 Ebay API 时,Content-Language header 会抛出 Misused header name 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53065928/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com