how to get data from API using curl ?

Not applicable

(I am new to curl and also apigee)

I am trying to get the Body data from API

"https://staging.api.pearson.com/content/scapi/urn:pearson:manifestation:014f333d-ceda-476f-81f2-d3d6c89ace42"

using curl with following command but unable to get any data than fault string

curl "https://staging.api.pearson.com/content/scapi/urn:pearson:manifestation:014f333d-ceda-476f-81f2-d3d6c89ace42"


Error that I am getting is :

{"fault":{"faultstring":"Failed to resolve API Key variable request.header.x-api key","detail":"errorcode":"steps.oauth.v2.FailedToResolveAPIKey"}}}

but when accessing the URL multiple times in Browser, I can able to get JSON data as desired

5049-capture-20170607-170606.png

Kindly somebody suggest to get JSON data

Sorry I am new to curl and also apigee

Thanks

1 2 3,737
2 REPLIES 2

Hi @Vimal --

I think I can point you in the right direction. However, when I hit your API in either the browser or with curl, I get the error.

The error message says that the API proxy needs to have a request header called "x-apikey" and that header must specify a valid API key. If you're unfamiliar with API keys, I recommend doing this tutorial. The tutorial will show you how to obtain the key and how to make API calls with it. It will also show you how the policy that enforces API keys is set up.

Your curl command would look something like this, where the -H flag sets the header that you need. You'll also have to provide a valid key (abc123456 is just an example). The tutorial mentioned above shows you how to get a key.

curl "https://staging.api.pearson.com/content/scapi/urn:pearson:manifestation:014f333d-ceda-476f-81f2-d3d6c89ace42" -H "x-apikey=abc123456"


It's also helpful to use the Trace tool in the Edge UI to trace an API call -- you can see what headers were passed in the request, the policies that execute, and much more. It might help with troubleshooting.

I hope this helps -- let me know if you can't get it working.

Will

Oh, and @Vimal - the reason it is working in the browser, but not working in curl, may be because the API itself is examining the cookie, that is implicitly sent by the browser, but not sent by the curl command. It is possible that the API implementation looks for either the cookie or the API key. The browser may send the appropriate cookie, while the curl command you are currently using sends neither.

Will's suggestion is to send the API key explicitly with curl. If my theory is right, you could also accomplish the same by sending the cookie with curl. To get the cookie you would need to use a cookie extractor tool, something like this script if you are using Chrome.