Why does this API return error "401 Unauthorized" when attempting to POST to it?

I am able to retrieve data from this API (CommerceInterface), but cannot POST to it, and have tried modifying the formatting many times, but it's still not working correctly for posting to it. They provided sample code in CURL, PHP, and Python, but not for C#. Does anyone have any idea of how to get this working? Thanks!

Endpoint - https://scm.commerceinterface.com/api/v2/mark_exported?

Python sample code:
    import requests #requires "requests" package
    import json
    response = requests.post('https://scm.commerceinterface.com/api/v2/mark_exported', 
                           data={'supplier_id':'1', 'token':'xYRPKcoakMoiRzWgKLV5TqPSdNAaZQT', 
                                 'ci_lineitem_ids':json.dumps([54553919, 54553920])}).json()
    if response['success'] == True:
       #Successfully marked as exported (only items which are not already marked exported)
       pass
    else:
       pass

Open in new window

Here is the C# code:

     
                WebRequest request = WebRequest.Create(geturl);
                request.Method = "POST";
                string postData = string.Format("supplier_id={0}", supplierid);
                postData += string.Format("&token={0}", token);
                postData += "&'ci_lineitem_ids':[" + lineid + "]";
                Console.WriteLine(postData);
                byte[] byteArray = Encoding.UTF8.GetBytes(postData);
                request.ContentType = "application/json";
                request.ContentLength = byteArray.Length;
                Stream dataStream = request.GetRequestStream();
                dataStream.Write(byteArray, 0, byteArray.Length);
                dataStream.Close();

                
                WebResponse response = request.GetResponse();
                Console.WriteLine(((HttpWebResponse)response).StatusDescription);
                dataStream = response.GetResponseStream();
                StreamReader reader = new StreamReader(dataStream);
                string responseFromServer = reader.ReadToEnd();
                Console.WriteLine(responseFromServer);
                reader.Close();
                dataStream.Close();
                response.Close();

Open in new window

Jordan JohnsonAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

KimputerCommented:
You have to rewrite your code so the output will be something like:

POST / HTTP/1.1
Host: xx.xxx.com
Accept: */*
Content-Length: 395
Expect: 100-continue
Content-Type: multipart/form-data; boundary=------------------------c9c628b9699bbb3a

--------------------------c9c628b9699bbb3a
Content-Disposition: form-data; name="supplier_id"

1
--------------------------c9c628b9699bbb3a
Content-Disposition: form-data; name="token"

xYRPKcoakMoiRzWgKLV5TqPSdNAaZQT
--------------------------c9c628b9699bbb3a
Content-Disposition: form-data; name="ci_lineitem_ids"

[54553919,54553920]
--------------------------c9c628b9699bbb3a--


Please note the boundaries, new lines, etc and such.
I can't post example codes here, as other people have been working on it. But this google search will get you some sample code you can use and adjust:
c# WebRequest multi form data boundary

However, this is probably just one part of the problem. Even with the incorrect post data, I don't think the http error is correct. Is authentication necessary before you using the API? If not, did you use your own unique token/key?
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Services

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.