简体   繁体   English

使用cURL查询REST API

[英]Querying REST API using cURL

To begin with I am a novice, both with REST and cURL . 首先,我是RESTcURL的新手。 This is what I want to do: 这就是我想做的:

I want to query the Twitter Rest API using commandline (some bash script) and hence, I want to use cURL to do that. 我想使用命令行(某些bash脚本)查询Twitter Rest API,因此,我想使用cURL做到这一点。

I have been trying to search for some tutorial (with no luck) that explains how to use REST with cURL and how to form the headers, get/post and then how to receive the REST response and the response status, so that I can write my logic based on the response status. 我一直在尝试搜索一些教程(没有运气),该教程解释了如何将RESTcURL一起使用以及如何形成标题,获取/发布,然后如何接收REST响应和响应状态,以便编写我的逻辑基于响应状态。

I would greatly appreciate if you can give me a few examples on how to call the twitter REST API using curl and also how to access the REST response status. 如果您能为我提供一些示例,说明如何使用curl调用twitter REST API,以及如何访问REST响应状态,将不胜感激。

I tried calling twitter using this: 我尝试使用以下方法调用Twitter:

curl "http://api.twitter.com/1/statuses/user_timeline.json?include_entities=true&screen_name=$user_name&count=150"

And got a json which I was able to use in my program. 并得到了我可以在程序中使用的json。 But I am not sure how to access the rest response status codes (in order to make my implementation more reliable). 但是我不确定如何访问其余响应状态代码(以便使我的实现更可靠)。 Also, I am not sure about what other flags should I provide to the cURL while calling it. 另外,我不确定在调用cURL时应向cURL提供哪些其他标志。

PS: Just to add, if you think there is some other MUCH better alternative to retrieving data from REST API other that using cURL, then also please let me know. PS:补充一下,如果您认为除了使用cURL之外,还有其他一些更好的替代方法可以从REST API中检索数据,那么也请告诉我。 Though my application is not web based and is more data crunching oriented and that's why I chose commandline and bash. 尽管我的应用程序不是基于Web的,并且更面向数据处理,所以这就是我选择命令行和bash的原因。

curl -i will print out the header information: curl -i将打印出标题信息:

HTTP/1.1 200 OK
Date: Sat, 29 Dec 2012 07:34:00 GMT

I know it doesn't directly answer your question, but I'd suggest using Python instead of bash, for a number of reasons. 我知道它不能直接回答您的问题,但是出于多种原因,我建议使用Python代替bash。 And if you decide on that, I strongly recommend the marvellous Requests library: http://docs.python-requests.org 如果您决定这样做,我强烈建议您使用奇妙的请求库: http : //docs.python-requests.org

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM