Roro
Roro

Reputation: 311

C# Ebay API Post-Order check_eligibility using HttpWebRequest

I'm trying to use the check_eligibility call from the eBay Post-Order API in C# very unsuccessfully. Every time I get a bad response. Here is one way I've tried:

string url = "https://api.ebay.com/post-order/v2/cancellation/check_eligibility";
        HttpWebRequest cancelOrderRequest = (HttpWebRequest)WebRequest.Create(url);

        cancelOrderRequest.Headers.Add("Authorization", "TOKEN " + AuthToken);
        cancelOrderRequest.ContentType = "application/json"; 
        cancelOrderRequest.Accept = "application/json"; 
        cancelOrderRequest.Headers.Add("X-EBAY-C-MARKETPLACE-ID", "EBAY_US"); 

        cancelOrderRequest.Headers.Add("legacyOrderId", ebayFullOrderId);

        cancelOrderRequest.Method = "POST";


        HttpWebResponse response = (HttpWebResponse)cancelOrderRequest.GetResponse();

And here is another way I've tried:

string url = "https://api.ebay.com/post-order/v2/cancellation/check_eligibility";
        var httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
        httpWebRequest.ContentType = "application/json";
        httpWebRequest.Method = "POST";
        httpWebRequest.Headers.Add("Authorization", "TOKEN " + AuthToken);
        httpWebRequest.Headers.Add("X-EBAY-C-MARKETPLACE-ID", "EBAY_US");
        httpWebRequest.Accept = "application/json"; 

        using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
        {
            string json = "{\"legacyOrderId\":\"###-###\"";

            streamWriter.Write(json);
            streamWriter.Flush();
            streamWriter.Close();
        }

        var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
        using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
        {
            var result = streamReader.ReadToEnd();
        }

Regardless of which way I try both came back with the same message, "The remote server returned an error: (400) Bad Request." If anyone could point me in the right direction I'd greatly appreciate it. Thanks.

Upvotes: 2

Views: 795

Answers (2)

Roro
Roro

Reputation: 311

My second code example ended up being the correct way to solve my problem. What I realized was my json was slightly off. I was missing the extra } at the end. The below updated json syntax fixed my problem going with the code from my second example.

var json = "{\"legacyOrderId\":\"" + ebayFullOrderId + "\"}";

Upvotes: 2

Yuriy Zaletskyy
Yuriy Zaletskyy

Reputation: 5151

Please try this code:

private void MakeRequests()
{
    HttpWebResponse response;

    if (RequestEbay(out response))
    {
        response.Close();
    }
}

private bool RequestEbay(out HttpWebResponse response)
{
    response = null;

    try
    {
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://api.ebay.com/ws/api.dll");

        request.Headers.Add("X-EBAY-API-SITEID", @"0");
        request.Headers.Add("X-EBAY-API-COMPATIBILITY-LEVEL", @"967");
        request.Headers.Add("X-EBAY-API-CALL-NAME", @"GetOrders");

        request.Method = "POST";
        request.ServicePoint.Expect100Continue = false;

        string body = @"<?xml version=""1.0"" encoding=""utf-8""?>
        <GetOrdersRequest xmlns=""urn:ebay:apis:eBLBaseComponents"">
          <RequesterCredentials>
            <eBayAuthToken>!!!!!!!!!!!!!!!!YOUR EBAY TOKEN!!!!!!!!!!!!!!!!1</eBayAuthToken>
          </RequesterCredentials>
         <ErrorLanguage>en_US</ErrorLanguage>
         <WarningLevel>High</WarningLevel>
          <CreateTimeFrom>2016-12-01T19:09:02.768Z</CreateTimeFrom>
          <CreateTimeTo>2017-12-15T19:09:02.768Z</CreateTimeTo>
          <OrderRole>Seller</OrderRole>
        </GetOrdersRequest>";
                byte[] postBytes = System.Text.Encoding.UTF8.GetBytes(body);
                request.ContentLength = postBytes.Length;
                Stream stream = request.GetRequestStream();
                stream.Write(postBytes, 0, postBytes.Length);
                stream.Close();

                response = (HttpWebResponse)request.GetResponse();
    }
    catch (WebException e)
    {
        if (e.Status == WebExceptionStatus.ProtocolError) response = (HttpWebResponse)e.Response;
        else return false;
    }
    catch (Exception)
    {
        if(response != null) response.Close();
        return false;
    }

    return true;
}      

Upvotes: 0

Related Questions