HTTP request causes 401 and 500 return code

647 views Asked by At

I've been working on the Walmart API but I keep getting either the 401 error or the 500 error when I run the code:

 public void post()
    {
        byte[] data = Encoding.ASCII.GetBytes(
$"username={user}&password={password}");

        HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://marketplace.walmartapis.com/v2/feeds?feedType=item");
        request.Method = "POST";
        request.Accept = "application/xml;";
        request.ContentLength = data.Length;
        request.Headers.Add("WM_SVC.NAME", "Walmart Marketplace");
        request.Headers.Add(authId);
        request.Headers.Add("WM_CONSUMER.ID", user);
        request.Headers.Add( time);
        request.Headers.Add(CorId);
        using (Stream stream = request.GetRequestStream ())
        {
            stream.Write(data , 0, data.Length);
        }

        string responseContent = null;

        using (WebResponse response = request.GetResponse())
        {
            using (Stream stream = response.GetResponseStream())
            { 
                using (StreamReader sr99 = new StreamReader(stream))
                {
                    responseContent = sr99.ReadToEnd();
                }
            }
        }

        MessageBox.Show(responseContent);
    }

Where authID is a signature generated from a jar file provided by Walmart time is also generated from the jar file. CorID is a randomly generated number and user is the user id.

Here is the link that describes the header parameters. Did I miss something in my header?

https://developer.walmartapis.com/#getting-started

1

There are 1 answers

0
Nate M. On

There are multiple problems with your request. First, you are submitting a feed, but sending it as an application/xml when it should be a multipart/form-data request. Beyond this, your headers aren't set up properly and there is currently a major problem with submitting multipart/form-data requests to Walmart using C#. I have not seen a post from anyone successfully sending a feed to Walmart via C#. I am currently using C# to execute a batch file that then fires a modified version of the Walmart Java SDK which is capable of sending the multipart/form-data requests.

The reponse below is for any request other than feeds. I would start with the example listed below to get familiar with how you need to set your headers up. This is going to work for the majority of Walmart interfacing, but if the request is a feed style request, you will either need to come up with a better solution to the multipart/form-data issue, use the Java SDK, or wait for the C# SDK. If someone reads this and has a better answer as to how to submit feeds via C# exclusively I would love to hear about it!

Here is an example of an application/xml request that works.

string timestamp = CurrentTimeMillis().ToString().Trim();
string query = @"orders/"+poID+"/acknowledge";
string request = v3BaseUrl + query;  //Constructed URI

string stringToSign = consumerId     + "\n" +
                      request.Trim() + "\n" +
                      "POST"         + "\n" +
                      timestamp      + "\n";


string signedString = signData(stringToSign);  //Your signed string

HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(request);
webRequest.Accept = "application/xml";
webRequest.ContentType = "application/xml";
webRequest.Method = "POST";
webRequest.Headers.Add("WM_SVC.NAME", "Walmart Marketplace");
webRequest.Headers.Add("WM_SEC.AUTH_SIGNATURE", signedString);
webRequest.Headers.Add("WM_CONSUMER.ID", consumerId);
webRequest.Headers.Add("WM_SEC.TIMESTAMP", timestamp.ToString().Trim());
webRequest.Headers.Add("WM_QOS.CORRELATION_ID", Guid.NewGuid().ToString());
webRequest.Headers.Add("WM_CONSUMER.CHANNEL.TYPE", channelType);
webRequest.ContentLength = 0;
webRequest.Timeout = Timeout.Infinite;
webRequest.KeepAlive = true;

using (HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse())
{
    if (response.StatusCode == HttpStatusCode.OK)
    {
        success = true;
    }
}