Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Interacting with RESTful API's via Javascript?

to start off, I know C++, C#, Python, some Ruby, and basic Javascript. Anyway, my question revolves around how to interact with RESTful API's via Javascript. I haven't been able to find any good examples on various websites, and so I've come here.

So my basic question is: How do I interact with RESTful API's via JS? And where can I find out how to implement OAuth in JS? I know how to get my keys and such, just not how to actually code them in.

Below is an example of a twitter API status update run from my MAC terminal with curl:

curl -u username:password 
-d "my tweet" 
http://api.twitter.com/1/statuses/update.json

How can I implement this in Javascript (preferably with OAuth authentication)? This would at least start me going in the right direction.

Thanks so much!!

like image 937
Alex Avatar asked Mar 31 '10 02:03

Alex


People also ask

How do I interact with RESTful API?

Step #1 – Enter the URL of the API in the textbox of the tool. Step #2 – Select the HTTP method used for this API (GET, POST, PATCH, etc). Step #3 – Enter any headers if they are required in the Headers textbox. Step #4 – Pass the request body of the API in a key-value pair.

What is RESTful API in JavaScript?

A REST API is a way of easily accessing web services. When a RESTful API is called, the server will transfer to the client a representation of the state of the requested resource.


3 Answers

The problem is that you will need to use AJAX to query the remote REST API, and AJAX is only allowed to query resources on the same domain as the page. So, a request to api.twitter.com will fail because it is on a different domain than your server.

To correct this you will need to code your server to make the request to twitter. You can however create your own AJAX stubs that will accept data directly from your page, and then build / send requests to twitter server-side using data supplied by your client.

like image 186
Justin Ethier Avatar answered Oct 13 '22 00:10

Justin Ethier


Generally Justin's approach is the correct one, however if you must have your client script interact with the REST service then you can do it with JsonP. that's JSON data wrapped in a function call.

see this page how to do it http://www.ibm.com/developerworks/library/wa-aj-jsonp1/

like image 41
LiorH Avatar answered Oct 12 '22 23:10

LiorH


OAuth version 1.0 in JavaScript is a bad idea because you need to expose your application's secret key, by doing so you may be allowing anyone else to impersonate your application. OAuth 1.0 was intended for use with a server under your control. So your users can send their tokens to your server and then you fire off the request to twitter on their behalf.

OAuth 2.0 solves this though twitter does not support it yet.

If you really want OAuth 1.0 you use my plugin: https://github.com/jpillora/jquery.rest and also make the change specified in this GitHub issue

like image 30
jpillora Avatar answered Oct 12 '22 23:10

jpillora