Why does this attempt to POST to Rest API result in Internal Error 500?

I am able to successfully GET orders from this API, but unable to POST. I have tried numerous methods to POST to this API, but everything results in an internal server error 500. Does anyone see what I am doing wrong from the PHP code given, and how it could be changed to work properly in C#? I am using fairly similar code to GET from the API.

Here is the C# code:

ASCIIEncoding encoding = new ASCIIEncoding();
Byte[] postBytes = encoding.GetBytes(jsondata);
// used to build entire input
StringBuilder sb = new StringBuilder();

// used on each read operation
byte[] buf = new byte[8192];

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(geturl);
//request.Credentials = new NetworkCredential(supplierid, token);
request.Method = "POST";
request.Accept = "application/json";
request.UserAgent = "curl/7.37.0";
request.ContentLength = postBytes.Length;
request.ContentType = "application/json";
SetBasicAuthHeader(request, supplierid, token);
Stream postStream = request.GetRequestStream();
postStream.Write(postBytes, 0, postBytes.Length);

HttpWebResponse response = request.GetResponse() as HttpWebResponse;
Stream resStream = response.GetResponseStream();

string tempString = null;
int count = 0;
// fill the buffer with data
count = resStream.Read(buf, 0, buf.Length);

// make sure we read some data
if (count != 0)
// translate from bytes to ASCII text
tempString = Encoding.ASCII.GetString(buf, 0, count);

// continue building the string
while (count > 0); // any more data to read?

catch (WebException ex)

Open in new window

Here is the code given in PHP:

// Create a new order
$username = "foo@foodomain.com";
$password = "qaz@PASSword.net";
$base_url = "https://my.freestylecommerce.com/webapi/V1/";

$data_json ='{"OrderNumber":"123495","OrderDate":"01/07/2015 5:06:39 AM","ShippingMethod":"2-Day Domestic","BillingAddress":{"FirstName":"Joe","LastName":"Test","Company":"","AddressLine1":"9 Sevilla Rd","AddressLine2":"","City":"Andover","State":"MA","Postcode":"01810","Country":"US","Email":"joet@yahoo.com",

"Phone":"9785551234"},"ShippingAddress":{"FirstName":"Joe","LastName":"Test","Company":"","AddressLine1":"9 Sevilla Rd","AddressLine2":"","City":"Andover","State":"MA","Postcode":"01810","Country":"US","Email":"joet@yahoo.com",

"Phone":"9785551234"},"UpdateAt":"01/07/2015 5:06:39 AM","OrderStatus":12,"TotalAmount":22.70,"PaidAmount":0.0,"ShippingAmount":6.95,"TaxAmount":0.00,"OrderItems":

$url = $base_url . "Orders";
print("<br/><br/>***** POST to URL ". $url . "<br/>");
$out = post_request ($data_json,  $url, $username, $password);

* Curl POST request

function post_request ($data_json, $url, $username, $password)

    $CURL = curl_init();

    // Set Curl Parameters
    curl_setopt($CURL, CURLOPT_URL, $url); // set url to post to
    curl_setopt($CURL, CURLOPT_HTTPHEADER, array('Accept: application/json','Content-Type: application/json','Content-Length: ' . strlen($data_json)));

    curl_setopt($CURL, CURLOPT_POST, 1);
    curl_setopt($CURL, CURLOPT_POSTFIELDS,$data_json);
    curl_setopt($CURL, CURLOPT_USERPWD, $username . ":" . $password);
    curl_setopt($CURL, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($CURL, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($CURL, CURLOPT_SSL_VERIFYHOST, 0);

        // Curl Response
    $Response = curl_exec($CURL);
    if($Response === false)
    return $Response;

Open in new window

Jordan JohnsonAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

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.

Dave BaldwinFixer of ProblemsCommented:
GET and POST are not interchangeable.  Why are you trying to POST to it if GET is the method that works?  Do you have docs that say you are supposed to be using POST?
Jordan JohnsonAuthor Commented:
I know that, I just mean to say that I was able to successfully GET but not POST.They are obviously not interchangeable, but I am trying to find why the POST is not working and what could be causing the issue.
Dave BaldwinFixer of ProblemsCommented:
Why do you think it should be working?  Do you know if it is supposed to accept a POST request?  It doesn't have to.
CompTIA Security+

Learn the essential functions of CompTIA Security+, which establishes the core knowledge required of any cybersecurity role and leads professionals into intermediate-level cybersecurity jobs.

Jordan JohnsonAuthor Commented:
It is the correct endpoint URL for posting to, so it should work as far as I know. I was wondering if anyone with a good understanding of PHP could show me what should be modified based upon the sample PHP code given for the API.
Dave BaldwinFixer of ProblemsCommented:
In the hundreds of 'curl' post pages I have online right now, I have never used CURLOPT_USERPWD.  I have always included them in the POST data in the form of '?name=username&password=password&...'

From http://php.net/manual/en/function.curl-setopt.php ...
CURLOPT_USERPWD       A username and password formatted as "[username]:[password]" to use for the connection.

You're being a little vague.  Do you have documentation for this API and it's POST format specifically?
Ray PaseurCommented:
Agree with Dave -- we need to see the API documentation, especially the part showing how they expect the POST-method requests to be formatted.
käµfm³d 👽Commented:
...but everything results in an internal server error 500
What do you mean by this? Are you saying the API returns a 500, or your own web server returns a 500?

According to the API specification, you would use a POST to create an order, and a GET to retrieve previously created orders.
Jordan JohnsonAuthor Commented:
Sorry, I will clarify. This was the error message that the API returned. I was able to successfully make a call to GET from the API, but the POST always results in the internal server error 500.

There is not much documentation on this, the only documentation given was the endpoint URL to post to, a sample order, and the PHP code. I utilized the sample order, and the information within it should not cause any issues. It seems to me that I need to possibly POST in a different manner, but I have not had any luck doing so thus far.
käµfm³d 👽Commented:
An internal server error is just that:  internal to the web server itself. In my experience, this means that there is an unhandled exception with the application. That's not something you can fix. I think you need to contact Freestyle Commerce and ask them what's going on with their API--at least in their test environment.

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
Ray PaseurCommented:
So we don't get to see the API documentation that we asked for?
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

From novice to tech pro — start learning today.