Archived Forum Post

Index of archived forum posts

Question:

SynchronousRequest bad charset ?

Sep 27 '12 at 10:15

CLEAR

LOCAL loReq

LOCAL loHttp

LOCAL lnSuccess

LOCAL lcRequestText

LOCAL loResp

loReq = CreateObject('Chilkat.HttpRequest')

loHttp = CreateObject('Chilkat.Http')

_password = 'komplex'

_user = 'alfeus'

lnSuccess = loHttp.UnlockComponent("xxx")

IF (lnSuccess <> 1) THEN

=MESSAGEBOX(loHttp.LastErrorText)

QUIT

ENDIF

loReq.HttpVerb = 'GET'

loReq.Charset = "utf-8"

loReq.ContentType = "application/jsonrequest"

loReq.SendCharset = 1

loReq.Path = "/api/customer/1870"

loReq.AddHeader("Content-Type","application/json")

loReq.AddHeader("charset","utf-8")

loReq.LoadBodyFromString(" ","utf-8")

loHttp.login = _user

loHttp.password = _password

loHttp.AcceptCharset = "utf-8"

port = 8443 ssl = 1

loResp = loHttp.SynchronousRequest("https://z1.net.e-net.sk",port,ssl,loReq)

IF (loResp = NULL ) THEN

? loHttp.LastErrorText

ELSE

? loResp.BodyStr

ENDIF

RELEASE loReq

RELEASE loHttp

RELEASE lnSuccess

RELEASE lcRequestText

RELEASE loResp

return

*return HHTP response (loResp.BodyStr) is :

{"_links": { "_update":{"href":"https://z1.net.e-net.sk:8443/api/customer/1870","method":"PUT"}, "self":{"href":"https://z1.net.e-net.sk:8443/api/customer/1870","method":"GET"}, "_delete":{"href":"https://z1.net.e-net.sk:8443/api/customer/1870","method":"DELETE"} } ,"status":"implementation","customer_name":"MariA!n KovA!Ä222","note":null }

but "customer_name":"Marián Kováč222"

SESSIONLOG:

---- Sending ---- GET /api/customer/1870 HTTP/1.1 charset: utf-8 Host: z1.net.e-net.sk:8443 Authorization: Basic YWxmZXVzOmtvbXBsZXg= Content-Length: 1 Content-Type: application/json; charset="utf-8"

---- Received ---- HTTP/1.1 200 OK Date: Thu, 27 Sep 2012 08:13:58 GMT Server: Apache/2.2.9 (Debian) mod_fastcgi/2.4.6 mod_ssl/2.2.9 OpenSSL/0.9.8n Vary: Content-Type Content-Length: 336 Content-Type: application/json

{"_links":{"_update":{"href":"https://z1.net.e-net.sk:8443/api/customer/1870","method":"PUT"},"self":{"href":"https://z1.net.e-net.sk:8443/api/customer/1870","method":"GET"},"_delete":{"href":"https://z1.net.e-net.sk:8443/api/customer/1870","method":"DELETE"}},"status":"implementation","customer_name":"Marián Kováč222","note":null}


Answer

Please try to maintain line-endings in your HTTP session logs.

The HTTP response in this case provides no indication of the charset for the bytes composing the body of the response. In other words, the "Content-Type" header of the response does not provide a "charset" attribute that tells a program receiving the response how to interpret the bytes of the body. Should the bytes be interpreted as utf-8? iso-8859-1? iso-8859-2? Etc. When NO charset is specified, the default according to MIME specifications is us-ascii.

The loResp.BodyStr property is a string, and in FoxPro (i.e. in ActiveX), strings are Unicode. Therefore, the bytes of the response body must be interpreted according to a specified character encoding. (See http://www.example-code.com/charset101.asp ) Given that no charset was specified, the default (us-ascii), according to MIME standards, is used. The accented chars ( áč ) have no representation in us-ascii, and therefore they are dropped.

The same problem is not seen with the SaveBodyText method because the bytes of the body are simply saved to the file with no attempt to interpret the bytes as characters.