2

I was going through the facebook docs and their they have used curl for making api calls

curl -G \
-d "fields=name" \
-d "access_token=<ACCESS_TOKEN>" \
"https://graph.facebook.com/<API_VERSION>/act_<AD_ACCOUNT_ID>/ads"

I have heard previously about the curl but never used it before

Now, I am big on axios, and I am thinking that this would be similar to restful api calls.

So to get the data from the given above snippet, I would need to make a get request since in the above snippet it says?

curl -G \

Secondly the given url is

"https://graph.facebook.com/<API_VERSION>/act_<AD_ACCOUNT_ID>/ads"

So axios equivalent would be

axios.get("https://graph.facebook.com/<API_VERSION>/act_<AD_ACCOUNT_ID>/ads")

and then -d i am guessing is for data? so my api request should look something like this?

axios.get("https://graph.facebook.com/<API_VERSION>/act_<AD_ACCOUNT_ID>/ads"
  ,data: {
     fields: "something",
      access_token:"8e8e8ee08e0e"  
    }
)

Can someone confirm that what I am doing is correct or not?

Alwaysblue
  • 9,948
  • 38
  • 121
  • 210
  • Are you wanting to run the JavaScript in a browser or via Node? – Phil Feb 24 '19 at 23:10
  • @Phil via browser but is there a difference when making request via axios from node or javascript. – Alwaysblue Feb 24 '19 at 23:27
  • Cross-domain requests come with their own set of challenges. See [Same origin Policy and CORS (Cross-origin resource sharing)](https://stackoverflow.com/questions/14681292/same-origin-policy-and-cors-cross-origin-resource-sharing) – Phil Feb 24 '19 at 23:29

1 Answers1

3

You're almost there...

Typically, using the -d option in curl forces a POST request with the -d values encoded into the request body. Setting the -G option forces a GET request with those data parameters encoded into the URL query parameters.

-G, --get
When used, this option will make all data specified with -d, --data, --data-binary or --data-urlencode to be used in an HTTP GET request instead of the POST request that otherwise would be used. The data will be appended to the URL with a '?' separator.

Axios separates query parameters into the params object, so you should use that instead of data.

axios.get(`https://graph.facebook.com/${apiVersion}/act_${adAccountId}/ads`, {
  params: {
    fields: "something",
    access_token:"8e8e8ee08e0e"  
  }
})
Phil
  • 157,677
  • 23
  • 242
  • 245
  • @phill what does -f stands for? -F 'name=My Ad Set'? this is from following facebook docs: https://developers.facebook.com/docs/marketing-api/buying-api – Alwaysblue Feb 26 '19 at 05:40
  • @anny123 not sure what you mean, that doesn't appear anywhere in my answer. If you're asking about the general use of `curl`, there's plenty of documentation available ~ https://curl.haxx.se/docs/manpage.html#-F – Phil Feb 26 '19 at 05:42