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,
*This post is locked for comments
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 :)
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
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');
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;
Share the code you are using to call the API
Hello Sulav,
Could you share with us how are you catching the response?
Thanks
I used UTF8 encoding for this. Below is the code.
stringContent := stringContent.StringContent(data,encoding.UTF8,'application/x-www-form-urlencoded');
How have you encoded the request message?
Stay up to date on forum activity by subscribing. You can also customize your in-app and email Notification settings across all subscriptions.
André Arnaud de Cal... 291,253 Super User 2024 Season 2
Martin Dráb 230,188 Most Valuable Professional
nmaenpaa 101,156