Page 1 of 1
How to add contents when using #PRIM_WEB.HttpRequest POST
Posted: Tue Apr 25, 2023 4:28 pm
by MegumiSawada
Hi All,
I am considering to send a file(e.g. Microsoft Excel) to php application by using #PRIM_WEB.HttpRequest POST method.
I would like to add contents like FormData.append() in XMLHttpRequest.
Could you please give me advice how to code it? Is there any sample application provided?
https://developer.mozilla.org/en-US/doc ... ta_Objects
Best Regards,
Megumi Sawada
Re: How to add contents when using #PRIM_WEB.HttpRequest POST
Posted: Wed Apr 26, 2023 7:32 am
by Dino
Hi Megumi,
Lets say, you have a welcome.php like this:
Code: Select all
<html>
<body>
Welcome <?php echo $_POST["name"]; ?><br>
Your email address is: <?php echo $_POST["email"]; ?>
</body>
</html>
and a html page that uses that php like this:
Code: Select all
<html>
<body>
<form action="welcome.php" method="post">
Name: <input type="text" name="name"><br>
E-mail: <input type="text" name="email"><br>
<input type="submit">
</form>
</body>
</html>
you can consume that welcome.php in a similar fashion from a server module for example like this:
Code: Select all
Begin_Com Role(*EXTENDS #PRIM_SRVM)
Srvroutine Name(SendPost)
* Group_Map For(*INPUT) Group(#fields)
Field_Map For(*OUTPUT) Field(#STD_NVARC)
Define_Com Class(#XPRIM_HttpRequest) Name(#Req)
Define_Com Class(#XPRIM_UriBuilder) Name(#Url)
#Url.SetScheme( 'https' )
#Url.SetHost( 'yoursite.com' )
#Url.SetPath( '/welcome.php' )
#Req.Content.AddUrlEncodedFormValue Name('name') Value('ichi')
#Req.Content.AddUrlEncodedFormValue Name('email') Value('ni')
#Req.DoPost Url(#Url)
If (#Req.Response.IsSuccessfulRequest)
#STD_NVARC := #Req.Response.AsString.AsNativeString
Else
#STD_NVARC := #Req.Response.ErrorCode.AsNativeString + ' ' + #Req.Response.ErrorMessage.AsNativeString
Endif
Endroutine
End_Com
and call that server module from a web page like this:
Code: Select all
Begin_Com Role(*EXTENDS #PRIM_WEB) Theme(#SYS_THEME<MaterialDesignBlue>)
Define_Com Class(#PRIM_MD.Label) Name(#Text) Caption('Lorem ipsum dolor sit amet, gravida ultrices, neque id, leo nonummy. Tellus imperdiet, vivamus mi ultricies, proin varius lorem. Lorem ultricies, ut wisi. Turpis dolore. A nonummy.') CaptionAlignment(TopLeft) DisplayPosition(1) Height(271) Left(21) Parent(#COM_OWNER) TabPosition(1) Top(34) Width(660) WordWrap(True)
Evtroutine Handling(#Com_owner.Initialize)
#COM_OWNER.PostSomething
Endroutine
Mthroutine Name(PostSomething)
Define_Com Class(#test425sm.SendPost) Name(#SendPost)
#SendPost.ExecuteAsync STD_NVARC(#Text)
Endroutine
End_Com
you will receive the response (in this case html page using that data) as a response.
Code: Select all
<html>
<body>
Welcome ichi<br>
Your email address is: ni
</body>
</html>
More details here:
https://docs.lansa.com/15/en/lansa018/c ... 1_0040.htm
Note there is an "AddFile" so to send a excel file it is possible, seems that something like this could work:
Code: Select all
#Req.Content.AddFile Path('c:\temp\abc.xlsx') contentinfo()
I don't have an example of a php receiving a file to test, neither idea of what is expected in contentinfo as content-type does not seems to work on that paramter. Will investigate a bit more.
Re: How to add contents when using #PRIM_WEB.HttpRequest POST
Posted: Wed Apr 26, 2023 8:49 am
by Dino
Re: How to add contents when using #PRIM_WEB.HttpRequest POST
Posted: Thu Apr 27, 2023 11:27 am
by MegumiSawada
Hi Dino,
Thank you! I'll try it.
Best Regards,
Megumi Sawada
Re: How to add contents when using #PRIM_WEB.HttpRequest POST
Posted: Fri Apr 28, 2023 1:27 am
by Dino
Still trying to figure out, but using this php example:
https://www.w3schools.com/php/php_file_upload.asp
and a LANSA web page with a filepicker like this:
Code: Select all
Define_Com Class(#PRIM_MD.FilePicker) Name(#FilePicker) Caption('Click to Upload File') DisplayPosition(2) Left(16) Parent(#COM_OWNER) TabPosition(2) ThemeDrawStyle('MediumTitle+Rounded') Top(16) Width(209)
* (...)
Evtroutine Handling(#FilePicker.FileSelected) File(#STD_BLOB)
Define_Com Class(#test425sm.SendPostWithImage) Name(#SendPostWithImage)
#SendPostWithImage.ExecuteAsync STD_NVARC(#Text) STD_BLOB(#STD_BLOB.Blob)
Endroutine
and a server routine like this:
Code: Select all
Srvroutine Name(SendPostWithImage)
Field_Map For(*INPUT) Field(#STD_BLOB)
Field_Map For(*OUTPUT) Field(#STD_NVARC)
Define_Com Class(#XPRIM_HttpRequest) Name(#Req)
Define_Com Class(#XPRIM_UriBuilder) Name(#Url)
Define_Com Class(#XPRIM_HttpContentInfo) Name(#MyContentInfo)
#Url.SetScheme( 'https' )
#Url.SetHost( 'yoursite' )
#Url.SetPath( '/tmp/upload.php' )
#MyContentInfo.MediaType := 'image/jpeg'
#MyContentInfo.Charset := "utf-8"
#MyContentInfo.ContentDisposition := "attachment"
#MyContentInfo.ContentDispositionFileName := "san.jpg"
#MyContentInfo.ItemName := "fileToUpload"
#Req.Content.AddFile Path(#STD_BLOB.FileName) ContentInfo(#MyContentInfo)
#Req.DoPost Url(#Url)
If (#Req.Response.IsSuccessfulRequest)
#STD_NVARC := #Req.Response.AsString.AsNativeString
Else
#STD_NVARC := #Req.Response.ErrorCode.AsNativeString + ' ' + #Req.Response.ErrorMessage.AsNativeString
Endif
Endroutine
seems to be closer to the answer. I just can't get it working completely here with restrictions in my php server. but the request.headers.log shows that the file is been transported:
Code: Select all
----------------------------------------------------------------------------------
| | HEADER NAME | HEADER VALUE |
----------------------------------------------------------------------------------
| 1 | Content-Disposition | attachment; name="fileToUpload"; filename="san.jpg" |
| 2 | Content-Length | 372715 |
| 3 | Content-Type | image/jpeg; charset=utf-8 |
| 4 | User-Agent | cpprestsdk/2.10.14 |
----------------------------------------------------------------------------------
Re: How to add contents when using #PRIM_WEB.HttpRequest POST
Posted: Fri May 19, 2023 5:27 pm
by MegumiSawada
HI Dino,
Thank you for your reply.
It helps us a lot!!
Best Regards,
Megumi Sawada