Warm tip: This article is reproduced from stackoverflow.com, please click
asp.net-mvc-4 c# ebay-api httpclient rest

Bad Request error setting header in Ebay API

发布于 2020-04-23 15:40:10

I would like to ask help how can I fix the issue in the header of my httpclient request. This is ebay restful api in creating a fulfillment shipment. I am able to create in Postman but when I tried it in VS, it won't work with error bad request. Screenshot below using postman.

enter image description here

Codes below in ASP.NET

private HttpClient CreateHttpClient()
{
    var client = new HttpClient();
    ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

    string baseAddress = WebApiBaseAddress;
    client.Timeout = new TimeSpan(0, 5, 59);
    client.BaseAddress = new Uri(baseAddress);
    client.DefaultRequestHeaders.Accept.Clear();
    client.DefaultRequestHeaders.Add("Authorization", string.Format("Bearer {0}", _cred.eBayToken));
    client.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/json; charset=utf-8");
    return client;
}

public HttpResponseMessage PostHttpResponse(string requestUri, object data)
{
    var stringPayload = JsonConvert.SerializeObject(data);
    var httpContent = new StringContent(stringPayload, Encoding.UTF8, "application/json");
    httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
    httpContent.Headers.Add("Content-Language", "en-US");


    using (var client = CreateHttpClient())
    {
        try
        {
            HttpResponseMessage response = client.PostAsJsonAsync(requestUri, httpContent).Result;
            if (response.IsSuccessStatusCode)
            {
                return response;
            }
            else
            {
                GetErrorsResponse(response);
                throw new HttpRequestException(string.Format("There was an exception trying to post a request. response: {0}", response.ReasonPhrase));
            }
        }
        catch (HttpRequestException ex)
        {
            throw ex;
            //return null;
        }
    }
}
Questioner
Jen143
Viewed
33
Jen143 2020-02-21 10:26

I was able to fix the issue by not converting the request to json but send as object. Though the error provided is very generic and could not identify the main issue. Upon asking to someone has experienced in ebay integration, the main issue is to provide all the needed in the headers.

 public HttpResponseMessage PostHttpResponse(string requestUri, object data)
{
    using (var client = CreateHttpClient())
    {
        try
        {
            HttpResponseMessage response = client.PostAsJsonAsync(requestUri, data).Result;
            if (response.IsSuccessStatusCode)
            {
                return response;
            }
            else
            {
                GetErrorsResponse(response);
                throw new HttpRequestException(string.Format("There was an exception trying to post a request. response: {0}", response.ReasonPhrase));
            }
        }
        catch (HttpRequestException ex)
        {
            throw ex;
            //return null;
        }
    }
}

And in the httpclient needs to add the header.

private HttpClient CreateHttpClient()
        {
            var client = new HttpClient();
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

            string baseAddress = WebApiBaseAddress;
            if (string.IsNullOrEmpty(baseAddress))
            {
                throw new HttpRequestException("There is no base address specified in the configuration file.");
            }
            client.Timeout = new TimeSpan(0, 5, 59);
            client.BaseAddress = new Uri(baseAddress);
            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Add("Authorization", string.Format("Bearer {0}", _cred.eBayToken));
            client.DefaultRequestHeaders.Add("Accept-Language", "en-US");
            client.DefaultRequestHeaders.Add("Accept-Charset", "utf-8");
            client.DefaultRequestHeaders.Add("Accept", "application/json");
            client.DefaultRequestHeaders.Add("LegacyUse", "true");
            return client;
        }