http_string not returning failure details

Discussions related to HTTPAPI (An HTTP Client Package for RPG programming.) http://www.scottklement.com/httpapi/
Post Reply
sbehera
Posts: 24
Joined: Tue Sep 07, 2021 10:46 pm

http_string not returning failure details

Post by sbehera »

Hello, i am doing http_string call as follow

Monitor;
result = http_string( 'POST': url: postdata: 'application/json')
On-Error *ALL;
If result <> ' ';
...
EndIf;
EndMon;

When there these is issue in the message i am sending, it raises exception and control goes to On-Error section and result field does not have any data. When i look at log there is failure cause returned from server :{"code":"S0001","message":["#/customs_form/customs_items/0/quantity: failed sche", i want to read this error message inside my program, please help how to do that?


Log:
senddoc(): entered
{ "request_id" : "F90-0000000092", "order_number" : "OOGP0007552362", "from_addr
recvresp(): entered
HTTP/1.1 422 Unprocessable Entity
Server: nginx/1.19.0
Date: Fri, 18 Mar 2022 19:14:38 GMT
Content-Type: application/json; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
SetError() #13: HTTP/1.1 422 Unprocessable Entity
recvresp(): end with 422
recvdoc parms: chunked 0
header_load_cookies() entered
recvchunk(): entered
get_chunk_size(): entered
b7

chunk size = 183
get_chunk_size returned 183
calling comm_blockread
{"code":"S0001","message":["#/customs_form/customs_items/0/quantity: failed sche
comm_blockread returned 183
Scott Klement
Site Admin
Posts: 635
Joined: Sun Jul 04, 2021 5:12 am

Re: http_string not returning failure details

Post by Scott Klement »

You could try calling http_req() to see if that works better for you.
sbehera
Posts: 24
Joined: Tue Sep 07, 2021 10:46 pm

Re: http_string not returning failure details

Post by sbehera »

http_req looks right option for me but little issue not able to figure out.
http_req expecting a variable in result string of type char with varying which i did. After i get result, i am using yajl_buf_load_tree to parse the value.

D rtnVal s 99999A varying
rc = http_req( 'POST': url: *omit : rtnVal : *omit : postdata)
JdsNode = yajl_buf_load_tree(%Addr(rtnVal) : %Len(rtnVal):
JdsRerr);

Error returns to JdsRerr --- parse error: premature EOF
sbehera
Posts: 24
Joined: Tue Sep 07, 2021 10:46 pm

Re: http_string not returning failure details

Post by sbehera »

I have made it to work by defining another fixed length character variable and moving data from rtnVal to this field and use it in call to yajl_buf_load_tree and is working but want to avoid this extra steps?


D result s 99999A Inz
D rtnVal s 99999A varying
rc = http_req( 'POST': url: *omit : rtnVal : *omit : postdata)
result = rtnVal;
JdsNode = yajl_buf_load_tree(%Addr(result) : %Len(%Trim(result)):
JdsRerr);
Scott Klement
Site Admin
Posts: 635
Joined: Sun Jul 04, 2021 5:12 am

Re: http_string not returning failure details

Post by Scott Klement »

A simpler solution is to load the tree from a string (rather than the memory buffer that is used under the covers to store your string data):

Code: Select all

rc = http_req( 'POST': url: *omit : rtnVal : *omit : postdata)
JdsNode = yajl_string_load_tree(rtnVal: JdsRerr):
But if you absolutely must use a buffer (for example, to deal with data larger than 2mb) then you need to make sure you're getting a pointer to the data in your buffer rather than the length.

Code: Select all

rc = http_req( 'POST': url: *omit : rtnVal : *omit : postdata)
JdsNode = yajl_buf_load_tree(%Addr(rtnVal:*data) : %Len(rtnVal): JdsRerr);
But, I think loading from a string is much cleaner. Also, I would highly discourage you from using %TRIM.
sbehera
Posts: 24
Joined: Tue Sep 07, 2021 10:46 pm

Re: http_string not returning failure details

Post by sbehera »

Works perfectly, thanks for help.
Post Reply