cancel
Showing results for 
Search instead for 
Did you mean: 

using C# to get orders via web API

using C# to get orders via web API

Hi Magento team,

 

This is first time I connect to Mag V2, 
I try to use WebRequest to get orders from Magento but always receive the response "Bad Request"

 

This is my code,

 

 

 static void Main(string[] args)
{
try
{
string url = "http://xxxx.com/rest/V1/orders";
HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create(url);
myReq.Method = "GET";
myReq.Accept = "application/json";
myReq.ContentType = "application/json";
myReq.Headers["Authorization"] = "Bearer g75nbtmoh4mt1y26i8yyx52pr6skr0eq"; //+Convert.ToBase64String(Encoding.ASCII.GetBytes(credentials));
WebResponse wr = myReq.GetResponse();

Stream receiveStream = wr.GetResponseStream();
StreamReader reader = new StreamReader(receiveStream, Encoding.UTF8);
string content = reader.ReadToEnd();
Console.WriteLine(content);
var json = "[" + content + "]"; // change this to array
var objects = JArray.Parse(json); // parse as array 
foreach (JObject o in objects.Children<JObject>())
{
foreach (JProperty p in o.Properties())
{
string name = p.Name;
string value = p.Value.ToString();
Console.Write(name + ": " + value);
}
}
Console.ReadLine();
}
catch (Exception ex)
{
Console.Write(ex.Message);

}
Console.Read();
}

 

 

But If I use URL "/rest/V1/customerGroups/default" then I can get data successfully

 

i'm using Magento ver. 2.1.0,

 

Do we have any other solution to get orders via Web API using C#?

 

Thanks,

Lam Nguyen