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();
}
Here is the example 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;
}
?>
according to PHP's site, http://php.net/manual/en/function.curl-setopt.php
CURLOPT_POSTFIELDS: This parameter can either be passed as a urlencoded string like 'para1=val1¶2=val2&...' or as an array with the field name as key and field data as value.
You can use either json_decode to get an array or http_build_query to get a urlencoded string