简体   繁体   中英

Curl request in Ruby for Facebook Api

I'm trying to make a curl call with curb gem equivalent to:

curl \
  -F 'name=My new CA' \
  -F 'subtype=CUSTOM' \
  -F 'description=People who bought from my website' \
  -F 'access_token=<ACCESS_TOKEN>' \
  https://graph.facebook.com/v2.5/act_<AD_ACCOUNT_ID>/customaudiences

So far my code looks like:

cr = Curl::Easy.http_post("https://graph.facebook.com/v2.5/act_XXXXXXXXXXXXXXX/customaudiences?access_token=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx") do |curl|
    curl.headers['name']='My new CA'
    curl.headers['subtype']='CUSTOM'
    curl.headers['description']='People who bought from my website'
    curl.headers['access_token']='xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
end

   pp cr.body_str

However, as a response I get this:

 => "{\"error\":{\"message\":\"(#100) Missing parameter(s): subtype\",\"type\":\"OAuthException\",\"code\":100,\"fbtrace_id\":\"BOd\\/mmhQkkP\"}}"

Could someone explain me what am I doing wrong? Thank you!

You can also just use a system call like this:

name = "something"
url = "http://www.example.com"
result = `curl -F 'name=#{name}' #{url}`

result will hold the output of your system call. For more sophisticated http requests, you probably want to take a look at faraday ( https://github.com/lostisland/faraday )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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