Retrieving API request body

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
Catt
Posts: 2
Joined: Mon Jun 10, 2019 8:00 pm

Retrieving API request body

Post by Catt » Mon Jun 10, 2019 8:09 pm

Currently creating a web API for publishing, however having issues coding to access the request body. At present I have the response defined in the server routine definition like : Srvroutine Name(APItut4) Response(*HTTP #uoHTTP4)

I've tried using variations of the .request.content intrinsic on #uoHTTP4, however can't seem to get anything working. Has anyone else had any luck being able to access (and process) the body from a request?

Thanks

jyoung
Posts: 634
Joined: Thu Jan 21, 2016 6:43 am
Location: Oklahoma City, OK USA

Re: Retrieving API request body

Post by jyoung » Tue Jun 11, 2019 12:07 am

If you are trying to read the body of a request, and its JSON, you can use the PRIM_JSON_Document.

Here is an example of reading POSTDATA from a request. The POSTDATA contains an array of "links".
Note the APIContext seen here is a helper class that formats the proper response codes and messages.

Code: Select all

define_map for(*INPUT) class(#PRIM_SRVM.HttpContext) name(#pContext) pass(*BY_REFERENCE)
define_com class(#PRIM_JSON.Document) name(#lDocument)
define_com class(#PRIM_JSON.Object) name(#lRequestObject) reference(*DYNAMIC)
define_com class(#PRIM_JSON.Member) name(#lRequestMember) reference(*DYNAMIC)
define_com class(#PRIM_JSON.Array) name(#lLinks) reference(*DYNAMIC)

define_com class(#PRIM_ACOL<#PRIM_ALPH>) name(#lMessages)

if (#pContext.Request.PathParameters.TryParseAsInt32( "wcn" #wk_WebControlNumber ) = False)
    #APIContext.BadRequest( #pContext "Missing Web Control Number" )
    return
endif

if (#wk_WebControlNumber = 0)
    #APIContext.BadRequest( #pContext "Invalid Web Control Number" )
    return
endif

#lDocument.LoadFromString( #pContext.Request.Content.FormVariables<"*POSTDATA">.GetText )

if (#lDocument.RootNode.IsNull)
    * no data in request body
    #APIContext.BadRequest( #pContext "Missing Request Body" )
    return
endif

* convert the root node to a json object
#lRequestObject <= #lDocument.RootNode *As #PRIM_JSON.Object

if (#lRequestObject *Is *NULL)
    * could not convert document to json
    #APIContext.BadRequest( #pContext "Could not convert Document to JSON" )
    return
endif

* now we can get the links member
#lRequestMember <= #lRequestObject<'Links'> *As #PRIM_JSON.Member

if (#lRequestMember *Is *NULL)
    * could not get links node from json 
    #APIContext.BadRequest( #pContext "Could not get links from JSON" )
    return
endif

* if the member is an array, we can 'cast' that member to an array
if ((#lRequestMember *IsNot *NULL) *AndIf (#lRequestMember.IsArray))
    #lLinks <= #lRequestMember.Value *As #PRIM_JSON.Array
else
    * missing links arrary in request body
    #APIContext.BadRequest( #pContext "Missing Links Array in Request Body" )
    return
endif


for each(#lLink) in(#lLinks)
    * do work with the links
endfor

* we're done
#APIContext.Ok( #pContext )
Hope this helps,
Joe

Catt
Posts: 2
Joined: Mon Jun 10, 2019 8:00 pm

Re: Retrieving API request body

Post by Catt » Thu Jun 20, 2019 6:00 pm

Thank you :)

Post Reply