Creating a POST request in VL-Web that can be consumed by a LANSA-generated API

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
naek
Posts: 5
Joined: Wed Dec 13, 2023 6:31 pm

Creating a POST request in VL-Web that can be consumed by a LANSA-generated API

Post by naek »

Hi,

I'm presently working on a project that requires utilizing a VL-generated web service. This service operates flawlessly during POSTMAN testing. However, when trying to connect with the service through VL-Web, I face a challenge: sending with parameters in String format instead of JSON, I tried working with #XPRIM_JsonObject but it's not supported by the web. Consequently, I have to manually parse the string upon transmission. Dealing with multiple inputs or outputs involves substring operations on the string, which isn't an ideal or efficient solution.

Part of my API :

Code: Select all

Srvroutine Name(CreateIntermediaire) Response(*HTTP £Context)

* Instance of the CreateIntermediaires Operation
Define_Com Class(£Com_Home.CreateIntermediaire) Name(£Operation)

* Instance of the IntermediaireObject Component used by Operation CreateIntermediaires
Define_Com Class(£Com_Home.IntermediaireObject) Name(£IntermediaireObject)

* Try to Bind the CreateIntermediaire Operation to the HTTP £Context.
If (£Operation.TryBind( £Context ))


Get Com(£Operation.Request.ContentJson) Com_Fields(£IntermediaireObject)

* If Identifier not included in the json and not an Identity Column then set value now
* £INTMED := *AUTONUMnn£INTMED

* Pre-check data set and automatically generate a 400 response if any errors. All messages are collected and send back using the "xUnexpectedError" Schema
Begincheck

Filecheck Field(£INTMED) Using_File(INTERMED) Using_Key(£INTMED) Found(*ERROR) Not_Found(*NEXT) Msgtxt("Intermediaire with ID " + £INTMED.asstring + " already exists")

Insert Fields(£IntermediaireObject) To_File(INTERMED) Io_Error(*NEXT) Val_Error(*NEXT) Check_Only(*YES)

If_Status Is_Not(*OKAY)

Message Msgtxt("Intermediaire with ID " + £INTMED.asstring + " includes invalid data")

Endif

Endcheck If_Error(*RETURN400)

Insert Fields(£IntermediaireObject) To_File(INTERMED) Io_Error(*NEXT) Val_Error(*NEXT)

If (£Operation.CheckIoStatus( £IO$STS ))

Set Com(£IntermediaireObject) Com_Fields(£IntermediaireObject)

£Operation.Response.SetContentJson( £IntermediaireObject )

Endif


Else

* Bad Response automatically handled by LANSA

Endif

Endroutine


Part of Webpage :

Code: Select all

Mthroutine Name(DoRequest)
Define_Com Class(£prim_web.HttpRequest) Name(£request)
Define_Com Class(£prim_web.json) Name(£json)
* Define_Com Class(£ws_back_client.Insert) Name(£ContactObject)

* Define_Com Class(£WS_Contact_V1.write)
£request.Content := '{"INTMED" :"' + £Edit.value + '","NAME":"' + £Edit3.Value + '"}'
£request.Content := £request.Content.Text
£sys_web.Alert Caption(£request.Content.Text)

£request.Url := ('http://localhost/FOR/ws_intermed/Intermediaires?=')

£request.Method := POST

£json.CreateRootObject


£request.Execute
 
Evtroutine Handling(£request.Completed)
If (£request.Response.StatusCode = 200)
£sys_web.Alert Caption("ALL good, client well added !")
£sys_web.Alert Caption("content response : " + £request.Response.Content.Text)

Else
£sys_web.Alert Caption("l'ajout a echoué code : " + £request.Response.StatusCode.AsString)
£sys_web.Alert Caption("content response : " + £request.Response.Content.Text)
Endif
Endroutine

Evtroutine Handling(£request.Failed)
Endroutine
Endroutine

Thanks in advance.

Naek
User avatar
Dino
Posts: 472
Joined: Fri Jul 19, 2019 7:49 am
Location: Robbinsville, NC
Contact:

Re: Creating a POST request in VL-Web that can be consumed by a LANSA-generated API

Post by Dino »

Hi

Have you checked this post?

viewtopic.php?f=4&t=2617

Kind regards
naek
Posts: 5
Joined: Wed Dec 13, 2023 6:31 pm

Re: Creating a POST request in VL-Web that can be consumed by a LANSA-generated API

Post by naek »

Thank you Dino for your response.

After reviewing the post, I'm unsure whether I should create a separate server module or a reusable component to handle constructing requests based on the inputs within my web page.
The message from VL regarding XPRIM_JsonObject not being supported for web use has left me uncertain.
Despite the successful API call from Postman, I'm facing challenges implementing the POST method directly within the web page.

Best regards

Naek
Jurgen.Rentinck
Posts: 11
Joined: Wed Nov 25, 2015 9:02 pm
Location: Amsterdam
Contact:

Re: Creating a POST request in VL-Web that can be consumed by a LANSA-generated API

Post by Jurgen.Rentinck »

Hi,

In a webpage you can use the PRIM_WEB.Json for creating a JSON document.

for example

Define_Com Class(#prim_web.Json) Name(#uJson)
Define_Com Class(#prim_web.JsonObject) Name(#uJsonObj) Reference(*DYNAMIC)

#uJsonObj <= #uJson.CreateRootObject
#uJsonObj.InsertString Key("sample") String("sample value")
Post Reply