How can I make a PUT request to an API framework

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
dbcbos
Posts: 23
Joined: Sun Jun 24, 2012 9:49 am

How can I make a PUT request to an API framework

Post by dbcbos » Thu Aug 17, 2017 10:13 am

Question: How can I make a PUT request to an URL?

I have done the following:

Code: Select all

   put "Authorization: Basic " & tUsr & tPwd & cr &\
   "Content-Type: application/json" & cr &\ 
   "PUT /_db/key2publish/_api/cursor/13499179 HTTP/1.1" & cr &\
   "Host: localhost:8529" into tHeaders
   set the httpHeaders to tHeaders
   put URL tURL into tContent
tUsr, tPwd, tURL contents are all verified working with Postmen

Content of the libURLLastHTTPHeaders(): (As you can see where there is a GET there should be a PUT)

Code: Select all

GET /_db/key2publish/_api/cursor/13514325 HTTP/1.1
Host: localhost:8529
Accept: */*
User-Agent: LiveCode (Win32)
Authorization: Basic cm9vdDprZXkycHVibGlzaA==
Content-Type: application/json
Response of the API (which is correct GET is not supported for that URL, PUT is):

Code: Select all

{"error":true,"errorMessage":"method not supported","code":405,"errorNum":405}
I am working on Windows 10 and Livecode 8.1.5

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9802
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: How can I make a PUT request to an API framework

Post by FourthWorld » Thu Aug 17, 2017 4:20 pm

405 suggests the server is expecting something other than PUT. Does POST work? Double-check the API docs.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

dbcbos
Posts: 23
Joined: Sun Jun 24, 2012 9:49 am

Re: How can I make a PUT request to an API framework

Post by dbcbos » Thu Aug 24, 2017 11:27 am

Hello Richard,

Sorry to get with you back so late. Thank you for your answer.

I have used another API call that does not require PUT to solve this issue.

POST also did not work and setting the headers or customheaders did also not work.

I wonder if this is a bug, it does work in the Chrome App Postmen using PUT.

Everytime I manually set the headers I get from the Postmen app they get written back to either POST or GET

You could actually test it yourself by going to this url: https://docs.arangodb.com/3.1/HTTP/AqlQ ... rom-cursor

and download and install arangodb which I am connecting to

mimu
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 76
Joined: Tue Mar 05, 2013 7:00 pm
Location: Berlin
Contact:

Re: How can I make a PUT request to an API framework

Post by mimu » Tue Aug 29, 2017 11:03 pm

Perhaps this helps:

Code: Select all

command buildHeaderPut
   put "Content-Type: application/json" & cr \
         & "Accept: application/json" & cr \
         & "X-HTTP-Method-Override: PUT" & cr  into tHeaders
   put "Authorization: Basic " after tHeaders
   put base64encode(""& ":" & sAccessToken) into tH
   replace cr with "" in tH
   put th after  tHeaders
   set the httpHeaders to tHeaders
end buildHeaderPut

-- update DBdata Tablename, id_field, id_of_record_to_update,data_as_array -returns 0 if success

function wservice_Update ptable,pkey,precord,pdata
   local turl,tjson,tpost,tresp
   put JsonExport(pdata) into tjson
   put textencode(tjson,"UTF8") into tpost
   buildHeaderPut
   put "http://xxx.xxx.xxx.xxx:81/api/v2/bodata/_table/"&ptable&"/"&precord&"?id_field="&pkey into turl
   libUrlSetSSLVerification false
   post tpost to url turl 
   put JsonImport(it) into tresp
   put tresp["resource"][1]["id"] into tanswer
   if tresp["id"] is a number then
      return 0
   else 
      return "error updating db"
   end if
end wservice_Update

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3581
Joined: Mon Jan 22, 2007 7:36 am
Location: Berkeley, CA, US
Contact:

Re: How can I make a PUT request to an API framework

Post by mwieder » Thu Sep 28, 2017 12:52 am

dbcbos-

Sorry to come in late to this topic... I don't spend that much time in the forum.

You're using the wrong syntax if you want to issue a PUT. Your syntax of

Code: Select all

put URL tURL into tContent
issues an http GET command, and translates to

Code: Select all

GET url(tURL) ; put it into tContent
instead try

Code: Select all

put tContent into url(tURL)

MaxV
Posts: 1579
Joined: Tue May 28, 2013 2:20 pm
Location: Italy
Contact:

Re: How can I make a PUT request to an API framework

Post by MaxV » Tue Oct 03, 2017 2:44 pm

First of all the code should be like:

Code: Select all

put URL "http://127.0.0.1:8529/_db/key2publish/_api/cursor/13499179"
:D
Livecode Wiki: http://livecode.wikia.com
My blog: https://livecode-blogger.blogspot.com
To post code use this: http://tinyurl.com/ogp6d5w

MaxV
Posts: 1579
Joined: Tue May 28, 2013 2:20 pm
Location: Italy
Contact:

Re: How can I make a PUT request to an API framework

Post by MaxV » Tue Oct 03, 2017 2:52 pm

I just tested on ArangoDB and livecode works very well.
No changes on header, just use POST:

Code: Select all

put "max" into temp["username"]
put "maxp" into temp["password"]
put jsonexport(temp) into myauth
post myAuth to "http://127.0.0.1:8529/_open/auth"
put it
And I get:

Code: Select all

{"jwt":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE1MDk2MzA2OTQsImlhdCI6MS41MDcwMzg2OTQ3NDc0NTJlKzYsImlzcyI6ImFyYW5nb2RiIiwicHJlZmVycmVkX3VzZXJuYW1lIjoibWF4In0=.xJkJh45Eh4zPMJhpPCGrQVKl1IDrIs63R4UcoLXlwBc="}
:mrgreen:
Livecode Wiki: http://livecode.wikia.com
My blog: https://livecode-blogger.blogspot.com
To post code use this: http://tinyurl.com/ogp6d5w

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3581
Joined: Mon Jan 22, 2007 7:36 am
Location: Berkeley, CA, US
Contact:

Re: How can I make a PUT request to an API framework

Post by mwieder » Tue Oct 03, 2017 4:37 pm

MaxV-

Yes, the authentication transaction uses an http POST command, but other rest endpoints in the api require a PUT. You have indeed confirmed that LC works with arangoDB, but the http syntax for various endpoints differ, i.e., creating a new user is a POST request, granting access is a PUT request, limiting the amount of documents in a query is a PUT request, returning an index is a GET request. All of these should be easily doable in the context of LC code.

MaxV
Posts: 1579
Joined: Tue May 28, 2013 2:20 pm
Location: Italy
Contact:

Re: How can I make a PUT request to an API framework

Post by MaxV » Wed Oct 04, 2017 12:46 pm

I think that the problem is in Arango Documentation. I have problems also with CURL.
Try CouchDB, that is well supported by livecode http://livecode.wikia.com/wiki/CouchDB
Livecode Wiki: http://livecode.wikia.com
My blog: https://livecode-blogger.blogspot.com
To post code use this: http://tinyurl.com/ogp6d5w

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3581
Joined: Mon Jan 22, 2007 7:36 am
Location: Berkeley, CA, US
Contact:

Re: How can I make a PUT request to an API framework

Post by mwieder » Wed Oct 04, 2017 6:17 pm

CouchDB is indeed well supported by Greg Miller's Daybed library:
https://github.com/madpink/couchdb4livecode

Re curl, allow me to recommend httpie. For quite some time now I've been using httpie as an alternative to curl or wget. It has a much more intuitive (IMO) commandline syntax and is much more powerful (built-in JSON support, for one thing)
https://httpie.org/

...and Postman for exploratory testing of rest APIs from within a browser. Usually my first stop when faced with a new API. And Postman scripts are exportable to curl.
https://www.getpostman.com/

dbcbos
Posts: 23
Joined: Sun Jun 24, 2012 9:49 am

Re: How can I make a PUT request to an API framework

Post by dbcbos » Mon Oct 16, 2017 10:25 am

Thank you all for your input.

I have not tested your new solutions. I gave up using the cursor api and thus using PUT, I used normal post requests to get the data by adding a limit/steps implementation to a query. Which worked out great for the solution I was working on.

MaxV
Posts: 1579
Joined: Tue May 28, 2013 2:20 pm
Location: Italy
Contact:

Re: How can I make a PUT request to an API framework

Post by MaxV » Tue Oct 17, 2017 10:57 am

Wow, can you show some examples?
I'd like to add a guide on the Wiki, or you can do it yourself. :D
Livecode Wiki: http://livecode.wikia.com
My blog: https://livecode-blogger.blogspot.com
To post code use this: http://tinyurl.com/ogp6d5w

ramav
Posts: 20
Joined: Tue Jan 06, 2009 4:56 am

Re: How can I make a PUT request to an API framework

Post by ramav » Tue Dec 12, 2017 5:24 am

Hi all:
I think the problem (as noted by mwieder above) is the syntax. According to the newly printed LC Dictionary, the syntax for a RESTful (CRUD) PUT is
put "whatever" into URL "put_target_API_link"
I have tried this with a number of different API collections and it works.

On a related note, and going back to Richard Gaskin's comments in other threads,
there is no way to do a RESTful DELete in LiveCode, as is required by various API collections. I saw a solution somewhere which said, use raw <i>connect to socket</i> and roll one's own DEL, headers and all. Unfortunately, this does not seem to work in mobile! Any insights?

Post Reply

Return to “Getting Started with LiveCode - Experienced Developers”