Page 1 of 1

How to convert curl POST command to LC?

Posted: Mon Jul 27, 2015 12:22 am
by Diane
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

Re: How to convert curl POST command to LC?

Posted: Mon Jul 27, 2015 5:42 am
by Simon
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

Re: How to convert curl POST command to LC?

Posted: Mon Jul 27, 2015 7:46 am
by SparkOut
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.

Re: How to convert curl POST command to LC?

Posted: Tue Jul 28, 2015 6:37 am
by Diane
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.

Re: How to convert curl POST command to LC?

Posted: Tue Jul 28, 2015 8:06 am
by Mark
Hi,

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

Kind regards,

Mark

Re: How to convert curl POST command to LC?

Posted: Sun Aug 02, 2015 5:05 am
by Diane
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

Re: How to convert curl POST command to LC?

Posted: Sun Aug 02, 2015 10:11 am
by SparkOut
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.

Re: How to convert curl POST command to LC?

Posted: Sun Aug 02, 2015 8:37 pm
by Diane
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

Re: How to convert curl POST command to LC?

Posted: Sun Aug 02, 2015 10:39 pm
by SparkOut
Of course, send me a message by all means :)