Link to home
Start Free TrialLog in
Avatar of Jordan Johnson
Jordan Johnson

asked on

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);
postStream.Close();

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

string tempString = null;
int count = 0;
do
{
// 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);
Console.WriteLine(tempString);

// continue building the string
sb.Append(tempString);
}
}
while (count > 0); // any more data to read?
}

catch (WebException ex)
{   
Console.WriteLine(ex.ToString());
logger.Log(ex.ToString());
//Console.ReadLine();
}

Open in new window


Here is the code given in PHP:

<?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":
[{"ProductSKU":"PF7977","Quantity":"1.00","Price":"15.75","Discount":"0.00"}],"SalesChannelId":"f5defa6d-9566-4705-b201-a3e90170895a"}';

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

/*
* 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_HTTPAUTH, CURLAUTH_BASIC);
    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)
    {
        print(curl_error($CURL));
    }
    curl_close($CURL);
    return $Response;
}
?>

Open in new window

Avatar of Dave Baldwin
Dave Baldwin
Flag of United States of America image

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?
Avatar of Jordan Johnson
Jordan Johnson

ASKER

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.
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.
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.
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?
Agree with Dave -- we need to see the API documentation, especially the part showing how they expect the POST-method requests to be formatted.
...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.
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.
ASKER CERTIFIED SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
So we don't get to see the API documentation that we asked for?