1
votes

When I make a PUT request to CreateOrReplaceInventoryItem using the Ebay API, I get this error:

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

The below code throws the error.

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

If I remove the line:

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

Then I get a returned error object:

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

What am I doing wrong? Is there another way to set Content-Language that I'm unaware of?

1
Instead of Content-Language set Accept-Language header. client.DefaultRequestHeaders.Add("Accept-Language", "en-US"); front_end_dev

1 Answers

2
votes

As the Content-Language is a content header you should assign it in the HttpContent instance instead of DefaultRequestHeaders. Based on your code would be something like this:

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