How to convert curl POST command to LC?

Want to move your code and projects to LiveCode but don't know where to start?

Moderators: heatherlaine, Klaus, FourthWorld, robinmiller

Post Reply
Diane
Posts: 20
Joined: Wed Jun 01, 2011 10:02 pm

How to convert curl POST command to LC?

Post by Diane » Mon Jul 27, 2015 12:22 am

Hello Everyone,

I'm interested in converting an existing cURL command into Livecode, and once I get that working, adding a friendlier user input to it.
But I'm having problems getting the basic curl command to work.

Here are my questions:
Format questions:
1) the POST /body info is in json format. Do I need to include a library for this?
2) I have an auth token I need to pass into the header. Is it sufficient to put "quote" in front of it?
Or do I need to urlEncode it?

Setting data:
3) once I have my headers setup, I'm doing the following:

Code: Select all

set the httpHeaders to tHeaders
Then I'm constructing my tArgList with the POST /body data
Then I'm posting the data to the url and putting the response in the message box:

Code: Select all

post tArgList to url "http:my_url"
put it into message
My question is, does this sequence look correct?

Thanks, help is much appreciated! :D
Diane

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am
Location: Palo Alto

Re: How to convert curl POST command to LC?

Post by Simon » Mon Jul 27, 2015 5:42 am

Hi Diane,
I'm going to bump this thread for you.
I'm not an expert on POST so I'm not sure my answers will help you.
Here is my work with it;
http://forums.livecode.com/viewtopic.php?f=8&t=19144
I know that thread is about mobile and CGI but it was the headers that stumped me for a while.
As to your questions
1) no you shouldn't have to include any libraries for the post.
2) urlencoding doesn't hurt anything but might be overkill if the authentication is plain asc without spaces.

And yes, the sequence looks correct.

Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

SparkOut
Posts: 1793
Joined: Sun Sep 23, 2007 4:58 pm

Re: How to convert curl POST command to LC?

Post by SparkOut » Mon Jul 27, 2015 7:46 am

You don't need a library, but there is a libJSON library available which may make it easier for you to construct the correctly formatted post data.
The sequence looks correct, but I am not sure what you are doing with the auth token.
If you are adding the token to the headers then probably maybe you don't need either quote or urlencode, it's difficult to tell what you are putting where to generate the headers.
So um, yeah, mostly what Simon already said.

Diane
Posts: 20
Joined: Wed Jun 01, 2011 10:02 pm

Re: How to convert curl POST command to LC?

Post by Diane » Tue Jul 28, 2015 6:37 am

Thank you Simon, Thank you Sparkout,
I'll list my example here and maybe you can take a look?
I realize it doesn't make sense out of context, will post tomorrow
but wanted to let you know I saw your replies.

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5132
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Re: How to convert curl POST command to LC?

Post by Mark » Tue Jul 28, 2015 8:06 am

Hi,

Probably you need to urlEncode the JSON data as well as the auth code. Can you post the cURL command?

Kind regards,

Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

Diane
Posts: 20
Joined: Wed Jun 01, 2011 10:02 pm

Re: How to convert curl POST command to LC?

Post by Diane » Sun Aug 02, 2015 5:05 am

Hello Mark,
Thanks for your reply.
I'm going to post both cURL and Livecode, and that way you can see what I'm trying to convert.
It will probably make more sense that way. I've sanitized the data a little so the token and channelname are changed.
But otherwise its verbatim from my code.
Thank you everyone for your help! :D

Code: Select all

-- Curl command to convert to Livecode
curl -H "Authorization: Token 3d3c94af982eac488dfcb58d715fa1f1ffffffff" -H "Content-Type: application/json" -X POST \
-d "{ \"body\" : \"Hello Diane3\", \"message_type\" : \"text/plain\", \"expire\" : \"2015-08-02\" }"\
                 http://api.pushetta.com/api/pushes/"channelname"/

Code: Select all

-- Livecode example
on mouseUp
   --headers
   put quote & "Authorization: Token 3d3c94af982eac488dfcb58d715fa1f1ffffffff" & quote & CRLF into tHeaders
   put quote & "Content-Type: application/json" & quote & CRLF after tHeaders
   set the httpHeaders to tHeaders
   
   --Post body json
   put quote &  "body" & quote & ":" into tMsg
   put quote & "Hello Diane3" & quote after tMsg   
   put quote &  "message_type" & quote & ":" into tMsgType
   put quote & "text/plain" & quote after tMsgType
   put quote & "expire" & quote & ":" into tExpire
   put quote & "2015-08-02" & quote after tExpire      
   put  "{" & tMsg & "," & CRLF &  tMsgType & "," & CRLF & tExpire & "}" into tArgList 
   --answer tArgList with "OK"
   
   --Channel  
   put quote & "channelname" & quote into tChannel
   answer tChannel with "OK"

   --Post command 
   post tArgList to url "http://api.pushetta.com/api/pushes/tChannel/"
   put it into message
   
   set the httpHeaders to empty
   
end mouseUp

SparkOut
Posts: 1793
Joined: Sun Sep 23, 2007 4:58 pm

Re: How to convert curl POST command to LC?

Post by SparkOut » Sun Aug 02, 2015 10:11 am

Hi Diane
The only obvious thing I can see is in your post command

Code: Select all

post tArgList to url "http://api.pushetta.com/api/pushes/tChannel"
is mixing literals and variables.
Try

Code: Select all

post tArgList to URL (" http://api.pushetta.com/api/pushes/" & tChannel & "/")
also leave out the quotes in tChannel. You might need to urlEncode tChannel.

I can't test here on this phone but if you are still stuck I will delve a bit when I get chance and see if there is anything in the headers you need to allow for, or other factors in the code.

Diane
Posts: 20
Joined: Wed Jun 01, 2011 10:02 pm

Re: How to convert curl POST command to LC?

Post by Diane » Sun Aug 02, 2015 8:37 pm

Thank you SparkOut, but I could not get it to work putting in the changes you suggested.
Would you mind if I sent you the actual code via PM?
And will include my email so you can respond directly.
If I sent the actual data, you can invoke it via Livecode and it should send me a message, so I'll know if it works or not.

If I get this working, I'm happy to post my sample here.
I saw many POST examples, but none exactly the same as what I was trying to do.

Thanks again for your help! I appreciate it so much!
Diane :D

SparkOut
Posts: 1793
Joined: Sun Sep 23, 2007 4:58 pm

Re: How to convert curl POST command to LC?

Post by SparkOut » Sun Aug 02, 2015 10:39 pm

Of course, send me a message by all means :)

Post Reply

Return to “Converting to LiveCode”