arsenal
arsenal

Reputation: 24154

How to consume webservice using CURL command?

I am trying to consume webservices using unix CURL command but somehow I am getting request denied error.

I am trying to consume this webservice url-

http://maps.googleapis.com/maps/api/distancematrix/json?origins=95112&destinations=94000&mode=bicycling&sensor=false

I am able to open the above url in the browser and I am able to see the contents.

Now I am trying to consume the same webservice using CURL command but it's not working for me.

I am trying something like this-

curl http://maps.googleapis.com/maps/api/distancematrix/json?origins=95050&destinations=94087&mode=bicycling&sensor=false

Is there anything wrong I am doing? I need to extract text portion of distance object.

Can anyone help me with this?

Upvotes: 1

Views: 3699

Answers (2)

Burhan Khalid
Burhan Khalid

Reputation: 174624

The & character is special in the shell; so the command is executing fine, just being sent to the background. These numbers [1] 4373 [2] 4374 [3] 4375 are the process id's that are sent to the background. You can tell later on that they are finished with [1] Done. To avoid this, you should quote the URL.

You also need to supply the -o option to curl; because I assume you are trying to save the json file to be processed later:

curl -o bikes.json "http://maps.googleapis.com/maps/api/...."

You can also use wget which is designed especially for this:

wget "http://maps.googleapis.com/maps/api/...." -O bikes.json

Or, my personal favorite httpie:

http "http://maps.googleapis.com/maps/api..." > bikes.json

To parse json at the shell, you can use a tool like jsawk. However, I prefer the much more simpler:

curl -s "http://www.example.com/..." | python -mjson.tool | grep "distance"

Upvotes: 2

Samuel BR
Samuel BR

Reputation: 51

Curl just work for see and test data in a Server through an URL. To consume the webservice itself you need a programming language which parse the content JSON (or other) in the response of an HTTP Request like the url you paste here to a type (of the language, like Arrays, Dicts, or something else) that can be worked.

Upvotes: 0

Related Questions