How do I handle HTTP response 303 errors from APIs in Visual Lansa - #XPRIM_HttpRequest

This Q&A forum allows users to post and respond to "How Do I Do ....." questions. Please do not use to report (suspected) errors - you must use your regional help desk for this. The information contained in this forum has not been validated by LANSA and, as such, LANSA cannot guarantee the accuracy of the information.
Post Reply
ColinP
Posts: 9
Joined: Wed May 02, 2018 7:40 pm
Location: UK

How do I handle HTTP response 303 errors from APIs in Visual Lansa - #XPRIM_HttpRequest

Post by ColinP »

Hi Lansa forum

I am using the #XPRIM_UriBuilder, #XPRIM_HttpRequest and #XPRIM_RandomAccessJsonReader functionality within Visual Lansa v15.
I have previously used Lansa Integrator but have changed to using this new built in functionality for my projects for the last few years.
Usually web service APIs return a status of 200 when the job has run and the requested data has been returned.
However; one of the web service APIs I use often returns a status of 303 which has a description of "See Other". This is the correct HTTP procedure when a server is busy or a lot of data is requested.
When HTTP status 303 is returned a JSON response is also returned that has the URL I need to use retrieve the response once the API has finished e.g.:-
{
"RedirectCount": 0,
"ResultUrl": "[url removed for security]"
}

In Lansa Integrator I was able to read this response and do a new API GET using the URL in the "ResultUrl" field and this would return the resulting JSON.

However; in visual Lansa I can’t seem to get hold of the "ResultUrl" field. When I try to access the JSON fields using the #XPRIM_RandomAccessJsonReader functionality the "RedirectCount" and "ResultUrl" are blank.

I have turned on the Lansa API debugging and the "response.body.log" file just contains the following text (but not the JSON):-
“Redirecting until template execution has completed”

Has anybody else had this situation and can you help me with processing the 303 response data, in the same way I could in Lansa Integrator, so I can do a new API GET using the URL in the "ResultUrl" field.

Regards
Colin
jimwatterson
Posts: 56
Joined: Thu Jul 09, 2020 8:31 am

Re: How do I handle HTTP response 303 errors from APIs in Visual Lansa - #XPRIM_HttpRequest

Post by jimwatterson »

I can't answer your question, sorry, but how do you 'turn on the Lansa API debugging'? That's something I've never heard of
ColinP
Posts: 9
Joined: Wed May 02, 2018 7:40 pm
Location: UK

Re: How do I handle HTTP response 303 errors from APIs in Visual Lansa - #XPRIM_HttpRequest

Post by ColinP »

Hi Jim

I got the details from:-
https://docs.lansa.com/15/en/lansa018/i ... 1_0395.htm

I have the following code at the beginning of my function to turn the debugging.
DEFINE_COM CLASS(#XPRIM_OSUtil) NAME(#OSUtil)
#OSUtil.SetEnvironmentVariable NAME('LANSA_XLIB_CONFIG') VALUE('/LANSA_dcxpgmlib/lansaxlib.config.json')
#OSUtil.SetEnvironmentVariable NAME('LANSA_XLIB_TRACEPATH') VALUE('/tmp/S42PGMLIB/log.txt')

It creates log files in [iSeries IFS drive]:\tmp\lansaxlib_logs\HttpRequest. The directory and which logs are produced are controlled by the configuration files.

Make sure to turn the logging off when you no longer need them because they can create a lot of sub directories and files on your system.

Hope this helps
Regards
Colin
jimwatterson
Posts: 56
Joined: Thu Jul 09, 2020 8:31 am

Re: How do I handle HTTP response 303 errors from APIs in Visual Lansa - #XPRIM_HttpRequest

Post by jimwatterson »

Oh, thanks. I did know about this one but I associated it with the standard XPRIM stuff, Still good to know.
adale
Posts: 210
Joined: Wed Apr 08, 2020 9:18 pm
Location: Poplarville, MS

Re: How do I handle HTTP response 303 errors from APIs in Visual Lansa - #XPRIM_HttpRequest

Post by adale »

Colin,
Thanks for sharing your code snippet.
I was having trouble with hte logging doc, and your reference line of the LANSA_XLIB_TRACEPATH is what was needed.



ColinP wrote: Wed Jul 07, 2021 9:27 pm Hi Jim

I got the details from:-
https://docs.lansa.com/15/en/lansa018/i ... 1_0395.htm

I have the following code at the beginning of my function to turn the debugging.
DEFINE_COM CLASS(#XPRIM_OSUtil) NAME(#OSUtil)
#OSUtil.SetEnvironmentVariable NAME('LANSA_XLIB_CONFIG') VALUE('/LANSA_dcxpgmlib/lansaxlib.config.json')
#OSUtil.SetEnvironmentVariable NAME('LANSA_XLIB_TRACEPATH') VALUE('/tmp/S42PGMLIB/log.txt')

It creates log files in [iSeries IFS drive]:\tmp\lansaxlib_logs\HttpRequest. The directory and which logs are produced are controlled by the configuration files.

Make sure to turn the logging off when you no longer need them because they can create a lot of sub directories and files on your system.

Hope this helps
Regards
Colin
Arlyn Dale
Servias LLC
Speedlime
Posts: 43
Joined: Wed Feb 03, 2021 2:52 am

Re: How do I handle HTTP response 303 errors from APIs in Visual Lansa - #XPRIM_HttpRequest

Post by Speedlime »

I have implemented this logging as described above using example in the docs

{
"Common":
{

"logOutputDir": "c:\Windows\Temp\lansaxlib_logs"

},
"HttpRequest":

{

"logging":

{

"requestHeaders": true,

"requestBody": true,

"responseHeaders": true,

"responseBody": true

}

}

}

When I validate the json i get this
Parse error on line 5:
... "logOutputDir": "c:\Windows\Temp\lan
----------------------^
Expecting 'STRING', 'NUMBER', 'NULL', 'TRUE', 'FALSE', '{', '[', got 'undefined'

The logging works to a fashion I get this in my log file every time I try and run my API
Runtime error: Unknown escape character (offset = 48)
Runtime error: Unknown escape character (offset = 48)
Runtime error: Unknown escape character (offset = 48)
Runtime error: Unknown escape character (offset = 48)

Not sure if it is the json in the config causing this, but when I comment out the logging the API work, when I enable them it fails on the request

enabling the logging like this
If (*OSAPI = "IBMI")
#OSUtil.SetEnvironmentVariable Name('LANSA_XLIB_CONFIG') Value('/tmp/lansaxlib.configas400.json')
#OSUtil.SetEnvironmentVariable Name('LANSA_XLIB_TRACEPATH') Value('/LANSA_dcxpgmlib/tmp/log.txt')
Else
#OSUtil.SetEnvironmentVariable Name('LANSA_XLIB_CONFIG') Value('c:\temp\lansaxlib.config.json')
#OSUtil.SetEnvironmentVariable Name('LANSA_XLIB_TRACEPATH') Value('c:\temp\log.txt')
Endif

I am at a loss as I need to set up logging to test issues with my new API's to a third party. Any idea's would be much appreciated
Thanks
René Houba
Posts: 220
Joined: Thu Nov 26, 2015 7:03 am

Re: How do I handle HTTP response 303 errors from APIs in Visual Lansa - #XPRIM_HttpRequest

Post by René Houba »

Hi Speedlime,

You can use the Reusable Part that is part of the latest LANSA Technical Newsletter:
https://lansa.com/downloads/support/new ... 202022.pdf
HttpLoggingArticleNewsletter.PNG
HttpLoggingArticleNewsletter.PNG (284.29 KiB) Viewed 44053 times
Speedlime
Posts: 43
Joined: Wed Feb 03, 2021 2:52 am

Re: How do I handle HTTP response 303 errors from APIs in Visual Lansa - #XPRIM_HttpRequest

Post by Speedlime »

Thanks Rene, I will look at this and implement today and see if that resolves my issue
Regards
adale
Posts: 210
Joined: Wed Apr 08, 2020 9:18 pm
Location: Poplarville, MS

Re: How do I handle HTTP response 303 errors from APIs in Visual Lansa - #XPRIM_HttpRequest

Post by adale »

Speedlime,
I had experienced the same message "unknown escape character" when working with a new rest API request, and the trace logging didn't give me the exact reason. Here is how I solved my issue.

I build a simple VL web view with a submit button, and a text area field, and in a Server Module HTTPRequest I copied the json string into a UniCodeString text field, that then was displayed in the view's text field. This gave me the raw json string that was being submitted in the request.

I then used this tool (https://jsonlint.com) to check my json string.

In my case, it turns out that I had and extra comma (or missed an ending comma I don't remember now) in the parsed json.

____
* Send back raw json string to review
#IXO_JString := #Writer.AsString.AsNativeString

*** I also did the same thing with the json response into a second text field in the view to see the raw response back as well. For initial testing, this was quicker than going and pulling up the debug or trace response file once I got the request corrected.

* load json into unicode string
#UniCodeString := #HttpRequest.Response.AsJson
* Send back raw json string to review
#IXO_JStringR := #UniCodeString
_____


Hope this helps.
Arlyn Dale
Servias LLC
Speedlime
Posts: 43
Joined: Wed Feb 03, 2021 2:52 am

Re: How do I handle HTTP response 303 errors from APIs in Visual Lansa - #XPRIM_HttpRequest

Post by Speedlime »

Arlyn thanks for that, I can apply this to my call server function and serialise the Json In and out to UniCodeString variables at the appropriate places.

Rene the above logging works well and I tested it in a Form from PC and in a call server function on the server and it logged everything as described.
I did find that in debugging the form it logged the first time but not the second time, maybe the debugging session causes it issues, when run normally multiple calls to the api logged every time.

Thanks for the help.
User avatar
MARCOREMMEDATA
Posts: 14
Joined: Mon Apr 11, 2022 4:48 pm
Location: ITALIA
Contact:

Re: How do I handle HTTP response 303 errors from APIs in Visual Lansa - #XPRIM_HttpRequest

Post by MARCOREMMEDATA »

Hi the code work correctly but I've noted that don't trace body request where content are load from file.

These code generate the log file

request.general.log
request.headers.log
response.body.log
response.headers.log

but not
request.body.log

Code: Select all

DEFINE_COM CLASS(£XPRIM_HttpRequest) NAME(£Request)
DEFINE_COM CLASS(£XPRIM_OSUtil) NAME(£OSUtil)

£OSUtil.SetEnvironmentVariable NAME('LANSA_XLIB_CONFIG') VALUE('D:/APPO/logrequest/lansaxlib.config.json')

£Request.Clear

£Request.Options.AddHeader NAME("Content-Type") VALUE("multipart/form-data")
£Request.Content.AddFile PATH("D:\APPO\ciao.pdf")

£Request.DoPost URL("https://dmodadev.emmedata.local/httpsendroot/inbox/ciao.pdf")

lansaxlib.config.json

Code: Select all

{

  "Common":
  {

    "logOutputDir": "D:/APPO/logrequest/"

  },
  "HttpRequest":

  {

    "logging":

    {

      "requestHeaders": true,

      "requestBody": true,

      "responseHeaders": true,

      "responseBody": true

    }

  }

}

Any suggestion?
MARCO ROSSI | Software Developer Sr. - Software Production
BrendanB
Posts: 134
Joined: Tue Nov 24, 2015 10:29 am

Re: How do I handle HTTP response 303 errors from APIs in Visual Lansa - #XPRIM_HttpRequest

Post by BrendanB »

Marco,

you are not getting a request.body.log, because your REQUEST does NOT have a 'body'.

you are attaching a file, but that is not a body. (it is in fact base64encoded and sent as an attachment).

so you dont get a request.body.log because it would be empty in this situation.
Post Reply