Page 1 of 2

Looking for help with HTTP POST

Posted: Thu Mar 09, 2017 4:45 am
by UKMC
Dear all,

after searchig for hours (it's 4:37 a.m. now :-()), my body-batteries are empty.
In spite of this I hope there is a friendly guy out there who can translate the following HTML-script

<html>
<form enctype="multipart/form-data" action="http://xxx.com/a/b/" method="POST">
<!-- MAX_FILE_SIZE (in Bytes) must be given before input-Field, which loads the (graphic)file -->
<input type="hidden" name="MAX_FILE_SIZE" value="1048576" />
<!-- The name" of the Input-Field has to be "file" because it's the name of the POST-Parameter -->
choose graphic to read: <input name="file" type="file" />
<input type="submit" value="read file" />
</form>
</html>

into a LC POST-message that I can use in my app.

Best regards


Ulrich

P.S.: In the final constellation, the graphic can be out of the filesystem of the desktop-PC or made with the camera of a mobile device.
If this makes a difference in handling, it would be very helpful to get it known.

Re: Looking for help with HTTP POST

Posted: Thu Mar 09, 2017 6:23 am
by FourthWorld
Did you see the POST command in the Dictionary?

And the "answer file" command will handle to file picker.

Re: Looking for help with HTTP POST

Posted: Thu Mar 09, 2017 2:15 pm
by dave.kilroy
Hi Ulrich

For a very simple example of POSTing files to a server search for "simple POST demo" in the 'Sample Stacks' section of the LiveCode toolbar

Re: Looking for help with HTTP POST

Posted: Mon Mar 13, 2017 6:41 pm
by UKMC
Hi,
FourthWorld wrote:Did you see the POST command in the Dictionary?

And the "answer file" command will handle to file picker.
yes, I have seen this and worked through the lessons as well - but I'm sorry to say that I have not been so lucky to get it transformed the right way.

Can you give me support to transform this query into a LC-POST-command ?

I would be so grateful !!!

Best regards


Ulrich

Re: Looking for help with HTTP POST

Posted: Mon Mar 13, 2017 6:44 pm
by UKMC
Hi Kilroy,

thank you very much vor your tip.
dave.kilroy wrote:Hi Ulrich

For a very simple example of POSTing files to a server search for "simple POST demo" in the 'Sample Stacks' section of the LiveCode toolbar
I downloaded this example immediately and tried to find the correct solution for my problem - unfortunately without success...

Would it be possible for you to help me getting the right transformation ?

This would be great !!

Best regards


Ulrich

Re: Looking for help with HTTP POST

Posted: Tue Mar 14, 2017 10:49 am
by dave.kilroy
Hi Ulrich

It should work - I'm about to DM you the URL to use to test it on my shared server account (please don't post anything too enormous or malicious). Once you've confirmed it works on my server try getting it working with your own URL (remember, the script of the .lc file needed is in the comments of the stack scrip of my demo post stack)

Kind regards

Dave

Re: Looking for help with HTTP POST

Posted: Wed Mar 15, 2017 2:17 pm
by MaxV
Try this code:

Code: Select all

post "hello world" to URL "http://posttestserver.com/post.php"
launch URL (word -1 of line 2 of it)
and this:

Code: Select all

post "MyCar=1" to URL "http://posttestserver.com/post.php"
launch URL (word -1 of line 2 of it)

Re: Looking for help with HTTP POST

Posted: Sat Mar 18, 2017 9:22 pm
by UKMC
Hi MaxV,

many thanks for your both examples, they work perfect.

I just tried to format my html-form in this way, but unfortunately, I find no working format.

Here is the code I tried at last:
put "enctype="&quote&"multipart/form-data"&quote&space&\
"input name="&quote&"file"&quote&\
" type="&quote&"file"&quote&\
" input type="&quote&"submit"&quote&\
" value="&quote&urlencode(image_to_send)&quote into theParams
post theParams to URL "http://xxx.com/a/b/"

This does not work.

Would it be possible for you to extract the correct parametrization out of my html-form ?

This would be so helpful to me...

Best regards


Ulrich

Re: Looking for help with HTTP POST

Posted: Mon Mar 20, 2017 1:46 am
by MaxV
The easy way:
I think you need this:
########CODE to copy and paste#######
put "name=" & urlencode(tFileName) into theParams
put "&type=" & urlencode(tFileType) after theParams
put "&value=" & urlencode(base64encode(image_to_send)) after theParams
post theParams to URL "http://xxx.com/a/b/"
#####END OF CODE generated by http://tinyurl.com/j8xf3xq with livecode 9.0.0-dp-5#####

Note that tFileName, tFileType and image_to_send are variable containing what you want. Please also note that image_to_send must be filled with something like:

Code: Select all

put URL ("binfile:path/to/image/file.jpg) into image_to_send 
On the server image must be base64decoded.

The hard way
If you want to use the multipart type of POST, then use http://livecode.wikia.com/wiki/LibURLMultipartFormData

Re: Looking for help with HTTP POST

Posted: Mon Mar 20, 2017 9:55 am
by UKMC
Hi MaxV,

thanks for the code you sent.

I tried immediately, but I get the result that the message format is wrong (HTTP Code 400)

For the image_to_send, I took
put URL ("binfile:"&Pfad) into image_to_send (where Pfad is the local file path on my desktop)

What do you think has to be placed in tFileName and tFileType ? I (successless) tried:
+ file file
+ file png

Perhaps it is easier to analyze if you have the concrete szenario. Here it is

<html>
<form enctype="multipart/form-data" action="http://api.qrserver.com/v1/read-qr-code/" method="POST">
<!-- MAX_FILE_SIZE (maximale Dateigröße in Bytes) muss vor dem input-Feld
angegeben werden, über welches die QR Code Grafik hochgeladen wird -->
<input type="hidden" name="MAX_FILE_SIZE" value="1048576" />
<!-- Der "name" des Input-Felds muss "file" lauten, da es sich dabei um den
Namen des POST-Parameters handelt -->
Zu lesende / scannende QR Code-Grafik auswählen: <input name="file" type="file" />
<input type="submit" value="QR Code lesen" />
</form>
</html>

And here is the API for this service:
http://goqr.me/de/api/doc/read-qr-code/

Attached you find the image_to_send:

I hope you can help to find a solution.

Best Regards and many thanks for your further help in advance


Ulrich
MaxV wrote:The easy way:
I think you need this:
########CODE to copy and paste#######
put "name=" & urlencode(tFileName) into theParams
put "&type=" & urlencode(tFileType) after theParams
put "&value=" & urlencode(base64encode(image_to_send)) after theParams
post theParams to URL "http://xxx.com/a/b/"
#####END OF CODE generated by http://tinyurl.com/j8xf3xq with livecode 9.0.0-dp-5#####

Note that tFileName, tFileType and image_to_send are variable containing what you want. Please also note that image_to_send must be filled with something like:

Code: Select all

put URL ("binfile:path/to/image/file.jpg) into image_to_send 
On the server image must be base64decoded.

The hard way
If you want to use the multipart type of POST, then use http://livecode.wikia.com/wiki/LibURLMultipartFormData

Re: Looking for help with HTTP POST

Posted: Mon Mar 20, 2017 12:51 pm
by UKMC
Hi Dave,

some more explanations to the problem:

There is a free webservice which I want to use for decoding qr-codes
This webservice provides a POST-API which is described in the given HTML-Form (details are given in my last post in this forum)

So I see no other way to use this service than "simulating" the HTML-form.
I found some hints to do so in the web as well.

If there is a better way, I'm absolute grateful for it.
I hope, you can help me to bring this case to a solution.

Best regards


Ulrich

Re: Looking for help with HTTP POST

Posted: Mon Mar 20, 2017 3:03 pm
by MaxV
Now I see.
Well, you have to use the http://livecode.wikia.com/wiki/LibURLMultipartFormData

It works. I got: "sQuiRt library for LiveCode demonstration". Is it right?

This is the code:
########CODE to copy and paste#######
on mouseUp
put empty into tForm
put "http://api.qrserver.com/v1/read-qr-code/" into tUrl
put the filename of image 1 into tFile
put "<file>" & tFile into tFile
if libURLMultipartFormData(tForm, "nam","file","type","file", "file", tFile) is not empty then
answer it ##error
else
set the httpHeaders to line 1 of tForm
post line 2 to -1 of tForm to url tUrl
put it into field 1
## check the result, etc., here
set the httpHeaders to empty
end if
end mouseUp
#####END OF CODE generated by http://tinyurl.com/j8xf3xq with livecode 9.0.0-dp-6#####

And I attached a working example.
Image

Re: Looking for help with HTTP POST

Posted: Mon Mar 20, 2017 4:41 pm
by UKMC
MaxV,

thank you so much - you're a hero :-)

I guess that I'd hardly come to this solution which is available now.
Thanks for giving so tremendous support.

I hope I can give sometimes something back...

Best regards


Ulrich

Re: Looking for help with HTTP POST

Posted: Mon Mar 20, 2017 6:45 pm
by UKMC
Hi MaxV,

solution is perfect for use on desktop, but the function libURLMultipartFormData is not available for mobile platforms.

In spite of this, there is the last step "let it running mobile" to do.
As you can imagine, I have no idea for this as well.

Would you be so kind to have a look after it ?

Best regards


Ulrich

Re: Looking for help with HTTP POST

Posted: Wed Mar 22, 2017 12:55 am
by jiml
Ulrich,

If you're looking to scan QR codes then mergAV and mergXzing will do that without needing to call a remote API.
mergAV is faster.

Here are some threads:
https://www.google.com/url?q=http://lis ... 5t-cmFDoTg

https://www.google.com/url?q=http://lis ... ZXTna4wXeQ

caveat: not all OS platforms are supported.

Jim Lambert