Not able to add Authorization: Basic in HttpWebRequest header Navision 2013

ssp_D365
Member Posts: 3
Hi Everyone,
I am trying to get token using the POST method.
I have created the same request in Postman and I am getting token there.

in Nav, I am getting the below error
Microsoft Dynamics NAV
Break On Error Message:
A call to System.Net.HttpWebRequest.GetResponse failed with this message: The remote server returned an error: (401) Unauthorized.
OK
I think I have written the wrong code to insert Authorization: Basic aW378dgwgfiuweffhoefhK in HttpWebRequest header.
I am sharing my code below, Please help me if there is any mistake in the code or I am missing something
I am using nav 2013R2 and the Http codeunit is not available and headers.add method is also not there.
Thanks.
I am trying to get token using the POST method.
I have created the same request in Postman and I am getting token there.

in Nav, I am getting the below error
Microsoft Dynamics NAV
Break On Error Message:
A call to System.Net.HttpWebRequest.GetResponse failed with this message: The remote server returned an error: (401) Unauthorized.
OK
I think I have written the wrong code to insert Authorization: Basic aW378dgwgfiuweffhoefhK in HttpWebRequest header.
I am sharing my code below, Please help me if there is any mistake in the code or I am missing something
HttpWebReq := HttpWebReq.CreateHttp('https://intapppreprod2com/security/createJWTToken'); HttpWebReq.Timeout := 300000; HttpWebReq.Accept := 'application/json'; HttpWebReq.ContentType := 'application/json'; //HttpWebReq.Credentials := Credential.NetworkCredential('Authorization','Basic '+'aW378dgwgfiuweffhoefhK'); Credential :=Credentials.NetworkCredential('Authorization','Basic '+'aW378dgwgfiuweffhoefhK'); HttpWebReq.Credentials := Credentials; HttpWebReq.Method := 'POST'; ServicePointManager.SecurityProtocol(SecurityProtocol.Tls12); HttpWebReq.UseDefaultCredentials(FALSE); StreamWriter := StreamWriter.StreamWriter(HttpWebReq.GetRequestStream,Encoding.GetEncoding('utf-8')); BearerTokenRequest := '{"username":' + '"'+'cdnnator'+'",'+'"password":'+'"'+'123test'+'",'+'"sellerCode":'+'"'+'124657'+'","callerName":'+'"'+'test"'+'}'; MESSAGE(BearerTokenRequest); StreamWriter.Write(BearerTokenRequest); StreamWriter.Close; HttpWebResp := HttpWebReq.GetResponse;//(Error part) TempBlob.INIT; TempBlob.Blob.CREATEINSTREAM(ResponseInStream); HttpWebResp.GetResponseStream.CopyTo(ResponseInStream); HttpStatusCode := HttpWebResp.StatusCode; ResponseHeaders := HttpWebResp.Headers; ResponseInStream.READ(ResponseText); //ResponseToken := COPYSTR(ResponseText,STRPOS(ResponseText,'access_token')+STRLEN('access_token')+3,328); ResponseToken := COPYSTR(ResponseText,STRPOS(ResponseText,'access_token')+STRLEN('access_token')+3,STRPOS(ResponseText,',"token_type"')-19); MESSAGE(ResponseToken); MESSAGE('%1',ResponseText); EXIT(ResponseToken);
I am using nav 2013R2 and the Http codeunit is not available and headers.add method is also not there.
Thanks.
0
Best Answer
-
Ok, I have successfully wasted 4 hours.
HttpWebReq.Headers.Add('Authorization','Basic '+'aW378dgwgfiuweffhoefhK');
HttpWebReq.Headers.Add (Correct)=A Capital
HttpWebReq.Headers.add (Wrong) = a small
0
Answers
-
Ok, I have successfully wasted 4 hours.
HttpWebReq.Headers.Add('Authorization','Basic '+'aW378dgwgfiuweffhoefhK');
HttpWebReq.Headers.Add (Correct)=A Capital
HttpWebReq.Headers.add (Wrong) = a small
0 -
Hi
I Try to use for the first time.
Can you explain me your code.
I need to do a POST with authentification basic and two parameters in my body, one string and one file.
Thank you for your help.
0
Categories
- All Categories
- 73 General
- 73 Announcements
- 66.6K Microsoft Dynamics NAV
- 18.7K NAV Three Tier
- 38.4K NAV/Navision Classic Client
- 3.6K Navision Attain
- 2.4K Navision Financials
- 116 Navision DOS
- 851 Navision e-Commerce
- 1K NAV Tips & Tricks
- 772 NAV Dutch speaking only
- 617 NAV Courses, Exams & Certification
- 2K Microsoft Dynamics-Other
- 1.5K Dynamics AX
- 320 Dynamics CRM
- 111 Dynamics GP
- 10 Dynamics SL
- 1.5K Other
- 990 SQL General
- 383 SQL Performance
- 34 SQL Tips & Tricks
- 35 Design Patterns (General & Best Practices)
- 1 Architectural Patterns
- 10 Design Patterns
- 5 Implementation Patterns
- 53 3rd Party Products, Services & Events
- 1.6K General
- 1.1K General Chat
- 1.6K Website
- 83 Testing
- 1.2K Download section
- 23 How Tos section
- 252 Feedback
- 12 NAV TechDays 2013 Sessions
- 13 NAV TechDays 2012 Sessions