Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics NAV forum
Suggested answer

sending data using in headers using REST POST method

Posted on by 2

Hello All,

I am trying to send data using an external API. It needs the data to be sent in header itself. When I am trying to send the data. I get error message as 

Microsoft Dynamics NAV
---------------------------

A call to System.Threading.Tasks.Task`1[System.String].Result failed with this message: '"UTF-8"' is not a supported encoding name.
Parameter name: name
---------------------------
OK
---------------------------

Any help would be appreciated 

Thanks,

  • Suggested answer
    Guillem Padilla Profile Picture
    Guillem Padilla 425 on at
    RE: sending data using in headers using REST POST method

    Your code is sending charset correctly (charset=utf-8), it seems to be an error on API Response, surely the API is returning charset like this: charset=UTF8

    When you execute ReadAsStringAsync it validates the charset name (UTF8) and then you get the error.

    A workaround is to use ReadAsByteArrayAsync and parse the result.

    Share your progress with us :)

  • Suggested answer
    Muratvzr Profile Picture
    Muratvzr 743 on at
    RE: sending data using in headers using REST POST method

    Here is the code for I am using for calling rest api HttpWebRequestMgt Codeunit great for this also if you want to deal with json you can use you can look at this blog post

    https://dynamics.is/?p=2637

    BodyPassword := STRSUBSTNO(bodyt,GlobApiKey);

    Url := STRSUBSTNO('%1/token',BaseUrl);

    HttpWebRequestMgt.Initialize(Url);

    HttpWebRequestMgt.DisableUI;

    HttpWebRequestMgt.SetMethod('POST');

    HttpWebRequestMgt.SetContentType('application/json');

    HttpWebRequestMgt.AddBodyAsAsciiText(BodyPassword);

    TempBlob.INIT;

    TempBlob.Blob.CREATEINSTREAM(Instr);

    IF NOT HttpWebRequestMgt.GetResponse(Instr,HttpStatusCode,ResponseHeaders) THEN

     HttpWebRequestMgt.ProcessFaultResponse('');

    IF HttpStatusCode.ToString <> HttpStatusCode.OK.ToString THEN

     ERROR('Connection Error');

  • Sulav Thapaliya Profile Picture
    Sulav Thapaliya 2 on at
    RE: sending data using in headers using REST POST method

    SMSSetup.GET;

    SMSSetup.TESTFIELD(SMSSetup."SMS API URL");

    SMSSetup.TESTFIELD("SMS Authentication Token");

    SMSSetup.TESTFIELD(Identity);

    IF GUIALLOWED THEN

     Window.OPEN(Text000);

    data := 'auth_token=' + SMSSetup."SMS Authentication Token";

    data += '&from=' + SMSSetup.Identity;

    data += '&to='+PhoneNo;

    data += '&text='+COPYSTR(MessageText,1,SMSSetup."SMS Text Length");

    stringContent := stringContent.StringContent(data,encoding.UTF8,'application/x-www-form-urlencoded');

    ReturnValue := CallRESTWebService(SMSSetup."SMS API URL",'',SMSSetup.Method,stringContent,HttpResponseMessage);

    result := HttpResponseMessage.Content.ReadAsStringAsync.Result;

    IF GUIALLOWED THEN

     Window.CLOSE;

    IF NOT ReturnValue THEN

     EXIT;

    result := HttpResponseMessage.Content.ReadAsStringAsync.Result;

    MESSAGE('%1',result.ToString);

    separator := ',';

    resultParts := result.Split(separator.ToCharArray());

    statusCode := FORMAT(result);

    ResponseCode := COPYSTR(statusCode,STRPOS(statusCode,'response_code') + 16,3);

    IF (ResponseCode = '200') THEN BEGIN

     EXIT(TRUE);

    END

    ELSE BEGIN

     EXIT(FALSE);

    END;

  • Suggested answer
    Suresh Kulla Profile Picture
    Suresh Kulla 43,640 on at
    RE: sending data using in headers using REST POST method

    Share the code you are using to call the API

  • Guillem Padilla Profile Picture
    Guillem Padilla 425 on at
    RE: sending data using in headers using REST POST method

    Hello Sulav,

    Could you share with us how are you catching the response?

    Thanks

  • Sulav Thapaliya Profile Picture
    Sulav Thapaliya 2 on at
    RE: sending data using in headers using REST POST method

    I used UTF8 encoding for this. Below is the code.

    stringContent := stringContent.StringContent(data,encoding.UTF8,'application/x-www-form-urlencoded');

  • Suggested answer
    Stefano Demiliani Profile Picture
    Stefano Demiliani 37,160 Most Valuable Professional on at
    RE: sending data using in headers using REST POST method

    How have you encoded the request message?

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Anton Venter – Community Spotlight

Kudos to our October Community Star of the month!

Announcing Our 2024 Season 2 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Dynamics 365 Community Newsletter - September 2024

Check out the latest community news

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 290,552 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 228,552 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,148

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans