Error in rest service like "authorization 301 error" in ax 2012

Hi,

How to consume the rest api in ax 2012.

I have tried the below code but i getting error like “Authorization 301 error” in response line.

How to resolve the error

static void ODataservice(Args _args)  
 {  
   
   System.Net.HttpWebRequest    request;  
   System.Net.HttpWebResponse   response;  
   CLRObject            clrObj;  
   System.Byte[]          bytes;  
   System.Text.Encoding      utf8;  
   System.IO.Stream        requestStream, responseStream;  
   System.IO.StreamReader     streamReader;  
   System.Exception        ex;  
   System.Net.WebHeaderCollection httpHeader;  
   str               byteStr;  
   System.Byte[]          byteArray;  
   System.IO.Stream        stream;  
   System.IO.Stream        dataStream;  
   byteStr = strfmt('%1:%2', "USERNAME", "PASSWORD");  
  
   try  
   {  
     new InteropPermission(InteropKind::ClrInterop).assert();  
     httpHeader = new System.Net.WebHeaderCollection();  
     clrObj = System.Net.WebRequest::Create(url);  
     request = clrObj;  
     utf8 = System.Text.Encoding::get_UTF8();   
     request.set_KeepAlive(true);  
     request.set_ContentType("application/xml");  
     utf8    = System.Text.Encoding::get_UTF8();  
     byteArray  = utf8.GetBytes(byteStr);  
     byteStr   = System.Convert::ToBase64String(byteArray);  
     httpHeader.Add("Authorization", 'Basic ' + byteStr);  
     request.set_ContentType("text/xml; encoding='utf-8'");   
     request.set_Method("POST");  
     request.set_Headers(httpHeader);  
     requestStream = request.GetRequestStream();   
     response = request.GetResponse();  
     responseStream = response.GetResponseStream();  
     streamReader = new System.IO.StreamReader(responseStream);  
     responseXml = streamReader.ReadToEnd();  
     info(responseXml);  
   }  
   catch (Exception::CLRError)  
   {  
     //bp deviation documented  
     ex = CLRInterop::getLastException().GetBaseException();  
     error(ex.get_Message());  
   }  
   requestStream.Close();  
   streamReader.Close();  
   responseStream.Close();  
   response.Close();  
 }  

I see you’re displaying only the Message property of the exception. Check out if the object doesn’t contain any other useful information.

Also, make sure that the web service supports this kind of authentication and that the username and password are correct.

Hi Martin,

username and password is correct but while coming to the response line going to the exception part

“Check out if the object doesn’t contain any other useful information” can you elaborate this

CLR exceptions are not mere messages; they’re objects. Therefore you can look at the ex variable and check what type it is, what’s the StackTrace, if there isn’t anything in InnerException property and so on.

Hi Martin,

Can you give me some example code to consume the rest api in x++.

6685.error.PNG

I will check