Search code examples
c#posthttpwebrequestebay-api

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.


Solution

  • 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 + "\"}";