'Encoding HttpClient Genexus
I'm trying to do a POST with an endpoint. In Postman it is working fine. One of the fields in JSON needs to be send with line breaks.
In genexus I'm adding the line break with chr(10), but when I execute the httpclient, it gives me an error, the owner of the endpoint told the me that I should check if the encoding is UTF-8.
This is part of my code:
&CharEnter = chr(10)
&body = '{"tipoEmision": "normal ", "Csv":"'
&csv = 'AA001;150;' + &CharEnter
&csv += 'B001;1;Normal;;;' + &CharEnter
&csv += 'C001;1;Factura electrónica;001;001;' + &CharEnter
&body += &csv + '"}'
&HttpClient.AddHeader('Content-Type','application/json')
&HttpClient.AddHeader('Authorization',&authorization)
&HttpClient.AddString(&body)
&HttpClient.Execute('POST',&UrlString)
The error I'm getting is:
{"constante":{"codigo":1,"descripcion":"Ocurrió una excepción no esperada. Unable to translate bytes [F3] at index 208 from specified code page to Unicode."},"error":true,"mensaje":null}
Any idea how could I set the encoding to UTF-8??
Solution 1:[1]
Change to
&csv += 'B001;1;Normal;;;' + NewLine()
or
&CharEnter = chr(10)+chr(13)
Solution 2:[2]
According to the JSON.org site, newlines should be specified with a \n
and not the ASCII values for LF
or CR
.
The solution in your case would be to define &CharEnter
as:
&CharEnter = "\n"
I'm not sure it is needed, but in case the above code doesn't work, you may want to try escaping the backslash...
&CharEnter = "\\n"
Also, the \n
corresponds to the "linefeed". You may also need to send a "carriage return", \r
, but that depends on the server implementation...
Solution 3:[3]
I just found the problem. It was the special characters I was sending, no relation with line breaks. I removed the character รณ and it worked.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | gusbro |
Solution 2 | Marcos Crispino |
Solution 3 | ferlrp |