I am trying to consume set of web API's, and following (doing everything on test server)
https://dynamics.folio3.com/2014/06/23/consuming-rest-apis-dynamics-ax-2012/
but getting an error as below
The remote server returned an error: (401) Unauthorized.
Same API works fine when tested with Postman tool.
My code structure
static void TestAPIReq(Args _args) { System.Net.HttpWebResponse response; CLRObject clrObj; System.Exception ex; System.Net.WebHeaderCollection httpHeader; System.Net.HttpWebRequest request; Notes reqBodyStr, resBodyStr; System.IO.Stream requestStream, responseStream; System.IO.StreamWriter streamWriter; System.IO.StringWriter stringWriter; System.IO.StreamReader streamRead; try { new InteropPermission(InteropKind::ClrInterop).assert(); clrObj = System.Net.WebRequest::Create("https://einvoicinggstinAPI/6666666666"); //actuals replaced request = clrObj; // adding headers httpHeader = new System.Net.WebHeaderCollection(); httpHeader.Add("auth-token", "1.c3f7b1xyzz");//actuals replaced httpHeader.Add("prod", "EInvoice"); httpHeader.Add("owner_id", "xxxxxx");//actuals replaced httpHeader.Add("gstin", "6666666666");//actuals replaced //request.set_ContentType("application/json"); request.set_Method("GET"); //Req Stream //requestStream = request.GetRequestStream(); //streamWriter = new System.IO.StreamWriter(requestStream); //Prepare Json body //this.createJSONStrBody(apiInfo.ApiId); //reqBodyStr = "Test"; // writing JSON //streamWriter.Write(reqBodyStr); //streamWriter.Flush(); //streamWriter.Close(); //Response response = request.GetResponse(); >>>>point of ERROR streamRead = new System.IO.StreamReader(response.GetResponseStream()); //this.getAPIResponseKeyValues(); resBodyStr = streamRead.ToString(); info (resBodyStr); } catch { //exception ex = CLRInterop::getLastException().GetBaseException(); error(ex.get_Message()); } }
Tested OK When tested from postman
Aany help would be appreciable.