Error Unable to send Json API with special characters through socket

Ranjith

New Member
Gettng Json parse error on server side when sending a JSON API with special characters like Euro/dollar symbol.
A sample code used is given below

Code:
DEFINE INPUT  PARAMETER i-payload         AS LONGCHAR   NO-UNDO.
DEFINE OUTPUT PARAMETER i-out             AS LONGCHAR   NO-UNDO.
DEFINE VARIABLE myAddJson      AS JsonObject NO-UNDO.
DEFINE VARIABLE myAddJson2     AS JsonObject NO-UNDO.
DEFINE VARIABLE myAddJson3     AS JsonObject NO-UNDO.
DEFINE VARIABLE i-post        AS MEMPTR    NO-UNDO.
 
FIX-CODEPAGE(i-out) = "UTF-8".
 
dataParser = NEW ObjectModelParser().
datajson = CAST(dataParser:parse(i-payload ),JsonObject).

myAddJson  = NEW JsonObject().
myAddJson:ADD("payLoad",datajson).
                         
myAddJson:WRITE(i-out, TRUE, "UTF-8").


set-size(i-post) = LENGTH(i-out,"RAW") + 1.
put-string(i-post,1) = i-out.


CREATE SOCKET hSocket.

IF hSocket:CONNECTED()
    THEN hSocket:DISCONNECT().

IF NOT hSocket:CONNECT(v-con) THEN
DO:
    ASSIGN
        o-errmsg = "Unable to connect to host " + i-hostname
                  + " port " + STRING(i-portnum).
    DELETE OBJECT hSocket.
    RETURN.
END.

IF NOT hSocket:WRITE(i-post, 1, GET-SIZE(i-post) ) THEN
DO:
    SET-SIZE(i-post) = 0.
    ASSIGN
        o-errmsg = "Error writing to socket".
    hSocket:DISCONNECT().
    DELETE OBJECT hSocket.
    RETURN.
END.
[/code ]

/*** Sample special character data in json **/
{
          "creditName": "TEST Champagne - Dom PérignonP2 Vintage"
}


When tried to send same API from postman tool, API is successfully received at server side.no parse error.
 
Last edited:
Please either put [ code ] [ /code ] tags around your code or insert it using the code icon:
1715668154897.png
 
Code:
message "Code Page Internal" Session:cpinternal.
message "Code Page Stream" Session:cpstream.

Are you including a start up parameter of -cpinternal UTF-8 -cpstream UTF-8

If not, it might be defaulting to ISO8859-1 (latin-1) ¯\_(ツ)_/¯
 
Back
Top