Looking for help with HTTP POST

Bringing the internet highway into your project? Building FTP, HTTP, email, chat or other client solutions?

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Looking for help with HTTP POST

Postby UKMC » Thu Mar 09, 2017 4:45 am

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.
UKMC
 
Posts: 47
Joined: Sun Jan 08, 2017 12:01 pm

Re: Looking for help with HTTP POST

Postby FourthWorld » Thu Mar 09, 2017 6:23 am

Did you see the POST command in the Dictionary?

And the "answer file" command will handle to file picker.
Richard Gaskin
Community volunteer LiveCode Community Liaison

LiveCode development, training, and consulting services: Fourth World Systems: http://FourthWorld.com
LiveCode User Group on Facebook : http://FaceBook.com/groups/LiveCodeUsers/
FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
 
Posts: 5015
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles

Re: Looking for help with HTTP POST

Postby dave.kilroy » Thu Mar 09, 2017 2:15 pm

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
"...this is not the code you are looking for..."
dave.kilroy
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
 
Posts: 801
Joined: Wed Jun 24, 2009 1:17 pm
Location: Plymouth, UK

Re: Looking for help with HTTP POST

Postby UKMC » Mon Mar 13, 2017 6:41 pm

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
UKMC
 
Posts: 47
Joined: Sun Jan 08, 2017 12:01 pm

Re: Looking for help with HTTP POST

Postby UKMC » Mon Mar 13, 2017 6:44 pm

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
UKMC
 
Posts: 47
Joined: Sun Jan 08, 2017 12:01 pm

Re: Looking for help with HTTP POST

Postby dave.kilroy » Tue Mar 14, 2017 10:49 am

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
"...this is not the code you are looking for..."
dave.kilroy
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
 
Posts: 801
Joined: Wed Jun 24, 2009 1:17 pm
Location: Plymouth, UK

Re: Looking for help with HTTP POST

Postby MaxV » Wed Mar 15, 2017 2:17 pm

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)
Livecode Wiki: http://livecode.wikia.com
My blog: http://livecodeitalia.blogspot.it
To post code use this: http://tinyurl.com/ogp6d5w
MaxV
 
Posts: 1135
Joined: Tue May 28, 2013 2:20 pm
Location: Italy

Re: Looking for help with HTTP POST

Postby UKMC » Sat Mar 18, 2017 9:22 pm

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
UKMC
 
Posts: 47
Joined: Sun Jan 08, 2017 12:01 pm

Re: Looking for help with HTTP POST

Postby MaxV » Mon Mar 20, 2017 1:46 am

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
Livecode Wiki: http://livecode.wikia.com
My blog: http://livecodeitalia.blogspot.it
To post code use this: http://tinyurl.com/ogp6d5w
MaxV
 
Posts: 1135
Joined: Tue May 28, 2013 2:20 pm
Location: Italy

Re: Looking for help with HTTP POST

Postby UKMC » Mon Mar 20, 2017 9:55 am

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
Attachments
QR-Code.png
QR-Code.png (4.66 KiB) Viewed 161 times
UKMC
 
Posts: 47
Joined: Sun Jan 08, 2017 12:01 pm

Re: Looking for help with HTTP POST

Postby UKMC » Mon Mar 20, 2017 12:51 pm

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
UKMC
 
Posts: 47
Joined: Sun Jan 08, 2017 12:01 pm

Re: Looking for help with HTTP POST

Postby MaxV » Mon Mar 20, 2017 3:03 pm

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
Attachments
QR_Reader.livecode.zip
(1.18 KiB) Downloaded 2 times
Livecode Wiki: http://livecode.wikia.com
My blog: http://livecodeitalia.blogspot.it
To post code use this: http://tinyurl.com/ogp6d5w
MaxV
 
Posts: 1135
Joined: Tue May 28, 2013 2:20 pm
Location: Italy

Re: Looking for help with HTTP POST

Postby UKMC » Mon Mar 20, 2017 4:41 pm

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
UKMC
 
Posts: 47
Joined: Sun Jan 08, 2017 12:01 pm

Re: Looking for help with HTTP POST

Postby UKMC » Mon Mar 20, 2017 6:45 pm

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
UKMC
 
Posts: 47
Joined: Sun Jan 08, 2017 12:01 pm

Re: Looking for help with HTTP POST

Postby jiml » Wed Mar 22, 2017 12:55 am

jiml
 
Posts: 111
Joined: Sat Dec 09, 2006 1:27 am
Location: Los Angeles

Next

Return to Internet

Who is online

Users browsing this forum: No registered users and 1 guest