How to add contents when using #PRIM_WEB.HttpRequest POST

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
MegumiSawada
Posts: 79
Joined: Tue Mar 22, 2016 1:45 pm
Location: Tokyo, Japan

How to add contents when using #PRIM_WEB.HttpRequest POST

Post 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
User avatar
Dino
Posts: 472
Joined: Fri Jul 19, 2019 7:49 am
Location: Robbinsville, NC
Contact:

Re: How to add contents when using #PRIM_WEB.HttpRequest POST

Post 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.
User avatar
Dino
Posts: 472
Joined: Fri Jul 19, 2019 7:49 am
Location: Robbinsville, NC
Contact:

Re: How to add contents when using #PRIM_WEB.HttpRequest POST

Post by Dino »

In this post there are more details about content type:

viewtopic.php?f=3&t=1682&p=3935
viewtopic.php?f=3&t=1672&p=3898
MegumiSawada
Posts: 79
Joined: Tue Mar 22, 2016 1:45 pm
Location: Tokyo, Japan

Re: How to add contents when using #PRIM_WEB.HttpRequest POST

Post by MegumiSawada »

Hi Dino,

Thank you! I'll try it.

Best Regards,
Megumi Sawada
User avatar
Dino
Posts: 472
Joined: Fri Jul 19, 2019 7:49 am
Location: Robbinsville, NC
Contact:

Re: How to add contents when using #PRIM_WEB.HttpRequest POST

Post 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                                   |
----------------------------------------------------------------------------------
MegumiSawada
Posts: 79
Joined: Tue Mar 22, 2016 1:45 pm
Location: Tokyo, Japan

Re: How to add contents when using #PRIM_WEB.HttpRequest POST

Post by MegumiSawada »

HI Dino,

Thank you for your reply.
It helps us a lot!!

Best Regards,
Megumi Sawada
Post Reply