Question
· Jan 12, 2018

HTTP Response parsing error

Hello

I'm in the process of creating a TestClass which simulate a HTTP POST call to REST-based web
services and while I am having success, I'm struggling on how to parse out the results to a JSON
format

Here is down the code part which concerns parsing the HttpResponse.Data variable into JSON :

s httprequest=##class(%Net.HttpRequest).%New() 
s httprequest.Server="http://127.0.0.1"
s httprequest.Port="57772"
s httprequest.Https = 0
s httprequest.ContentType="application/json"
s httprequest.Authorization = "Basic X1N5c3RlbTpTWVM="
d httprequest.EntityBody.Write(json)
s sc = httprequest.Post("http://127.0.0.1:57772/api/v0/bwxpert/visiocheck/account")

Do $$$AssertStatusOK(sc,"HTTP POST OK")

Set Result= httprequest.HttpResponse.Data
Set DynamicObject = {}.%FromJSON(Request.HttpResponse.Data)

Do $$$AssertEquals(Result.syncType,"ADMIN_SYNC","sync OK")
 write DynamicObject.%ToJSON(),!

When run the tests on Terminal i get this httpresponse content displayed :

{"errors":[{"code":5035,"domain":"%ObjectErrors","error":"ERREUR #5035: Exception générale Nom 'Parsing error' Code '3' Données ''","id":"GeneralException","params":["Parsing error",3,""]}],"summary":"ERREUR #5035: Exception générale Nom 'Parsing error' Code '3' Données ''"}

Any idea about this issue ?
Thank you in advance

Discussion (3)0
Log in or sign up to continue

Second parameter test flag in method Post can help you to understand what you send and what do you receive from the destination server.

 If test is 1 then instead of connecting to a remote machine it will just output what it would have send to the web server to the current device, if test is 2 then it will output the response to the current device after the Post.

s sc = httprequest.Post("/api/v0/bwxpert/visiocheck/account", 1)

should show something like

POST /api/v0/bwxpert/visiocheck/account HTTP/1.1
User-Agent: Mozilla/4.0 (compatible; Cache;)
Host: 194.2.225.126
Accept-Encoding: gzip
Authorization: Basic X1N5c3RlbTpTWVM=
Content-Length: 2
Content-Type: application/json
 
{}

And I think in your case error in this line

d httprequest.EntityBody.Write(json)

Write method expects to see string, but I think you put some JSON object here. So, you have to change it.

d httprequest.EntityBody.Write(json.%ToJSON())

Some notice, put full address with server name and port to Post is redundant, while you already use this address for particular properties.

PS: It is not secure to publish IP to your publicly available server. As a moderator I replaced your IP to localhost, and fixed formatting.

Some possible code improvements:

1. Instead of:

s sc = httprequest.Post("http://127.0.0.1:57772/api/v0/bwxpert/visiocheck/account")

it's enough to specify:

s sc = httprequest.Post("/api/v0/bwxpert/visiocheck/account")

as the rest of the address is defined beforehand.

2. Instead of:

s httprequest.Authorization = "Basic X1N5c3RlbTpTWVM="

you can specify:

s httprequest.Username = "_SYSTEM"
s httprequest.Password = "SYS"

Which is more readable

3. When specifying server, protocol in not required, incorrect even:

s httprequest.Server="127.0.0.1"

4. Finally, don't use short syntax (s, d) and use one naming convention consistently - in your example some commands are in lowercase and some start with a capital letter.