Jordan Johnson
asked on
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:
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
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();
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.