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

ssp_D365ssp_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.
ofj1l1mdr06u.png

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.

Best Answer

  • ssp_D365ssp_D365 Member Posts: 3
    Answer ✓
    Ok, I have successfully wasted 4 hours.
    HttpWebReq.Headers.Add('Authorization','Basic '+'aW378dgwgfiuweffhoefhK');
    HttpWebReq.Headers.Add (Correct)=A Capital :D
    HttpWebReq.Headers.add (Wrong) = a small :'(:s


Answers

  • ssp_D365ssp_D365 Member Posts: 3
    Answer ✓
    Ok, I have successfully wasted 4 hours.
    HttpWebReq.Headers.Add('Authorization','Basic '+'aW378dgwgfiuweffhoefhK');
    HttpWebReq.Headers.Add (Correct)=A Capital :D
    HttpWebReq.Headers.add (Wrong) = a small :'(:s


  • casnicocasnico Member Posts: 1
    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.

    ciqmm6frt5uf.png

    lkqi01o31a3s.png

    Thank you for your help.
Sign In or Register to comment.