How to make "username" & "password" POST to a REST/HTTP API

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

Moderators: Klaus, FourthWorld, heatherlaine, kevinmiller, robinmiller

Post Reply
steveuba
Posts: 14
Joined: Sat Mar 22, 2014 5:10 pm
Location: Alberta
Contact:

How to make "username" & "password" POST to a REST/HTTP API

Post by steveuba » Sun Aug 07, 2016 1:25 am

Is there anyone with an idea on how I can accomplish this api call, using HTTP, in LiveCode;

Code: Select all

curl -u "{username}":"{password}" "https://gateway.missioncontrol.net/conversation/api/v1/{method}"
Am trying to wrap some "CURL" functionalities in "HTTP" to allow it being called on a mobile device, but where it fails for me is when I need to authenticate the user.

I have tried

Code: Select all

"POST tUsernameAndPassword to  https://gateway.missioncontrol.net/conversation/api/v1/feedback"
but this does not work, so I am looking for the equivalent commands to doing the earlier code example from above in LiveCode.

Any help is greatly appreciated.

Thanks,

Stephen
Custom IT solutions that won't require you to break the bank.......
http://www.ezektec.com/

shaosean
Posts: 878
Joined: Thu Nov 04, 2010 7:53 am

Re: How to make "username" & "password" POST to a REST/HTTP

Post by shaosean » Mon Aug 08, 2016 2:32 am

Depending on the type of authorization required, this might work for you.. Depending on the API, that POST might just be a GET..

Code: Select all

set the httpHeaders to "Authorization: Basic" && base64Encode("user:password")
POST <data> to  https://gateway.missioncontrol.net/conversation/api/v1/feedback

bhall2001
Posts: 99
Joined: Thu Oct 30, 2014 3:54 pm

Re: How to make "username" & "password" POST to a REST/HTTP

Post by bhall2001 » Thu Aug 18, 2016 12:49 pm

I've been doing a lot of work with Livecode and APIs. Here's what I'd suggest doing.

Get the API calls working outside of Livecode in a tool designed to let you exercise the API calls. My tool of choice is Postman. Look it up on Google and have at it. There's a bit of a learning curve but once you figure out all the options (and there are lots), it's pretty easy to take a curl syntax and get it up and running in Postman. Postman can even generate code for you to make the call in some common languages (sadly not Livecode).

Using Postman guarantees that you figure out how the API is supposed to work before you introduce Livecode. Once you get a few curl API calls going in LC, you'll be amazed at how quickly you can wrap an entire API library up in LC function calls.

Now that you've got the calls working in Postman, move to LC (if you can't get the API working in Postman, DO NOT move to Livecode).

Make sure you set the headers to be what you had in Postman. Make sure you know whether the API call is a Post or a Get. For username/password, you can set the Authorization header as suggested, you may need to include the username/password in the url. If this is the case look up POST in the LC Dictionary to read about the nuances of this.

Code: Select all

post someData to URL "https://username:password@www.example.com/"
(make sure you always use https ;-)

I've used APIs where the authorization is included in a JSON or XML payload that is sent in the data of the request (this is dependent on the API definition). The API spec should tell you how to authorize your request.

Postman really is an invaluable tool to verify the parameters needed for the API call, then it's a matter of translating the syntax into LC.

Bob

Post Reply

Return to “Internet”