You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Facebook创建广告组API(v2.12)以POST请求体传参失败求助

Hey there! Let's break down this Facebook Ads API v2.12 issue you're facing—where passing parameters in the POST request body isn't working, even though you've got a working example using query string params. I've seen this come up a lot, so let's walk through the fixes and best practices.

Why Your Query String Example Works (But Body Params Don't)

Your successful code shoves all parameters into the URL's query string, which works because the API accepts this style for POST requests. But when you try moving params to the request body, you're likely missing a critical piece: the correct Content-Type header, or formatting the body incorrectly.

Fix 1: Use the Right Content-Type & Format the Body Properly

Facebook Ads API v2.12 supports two main formats for POST body parameters:

  1. application/json (cleanest for complex data like targeting objects)
  2. application/x-www-form-urlencoded (for simpler key-value pairs)

Here's a working example using application/json with Node.js (using axios, but the logic applies to any HTTP client):

const axios = require('axios');

async function createAdSet(req) {
  try {
    const adSetData = {
      name: "My Reach Ad Set",
      optimization_goal: "REACH",
      billing_event: "IMPRESSIONS",
      bid_amount: 2,
      daily_budget: 1000,
      campaign_id: req.body.CAMPAIGN_ID,
      targeting: {
        geo_locations: {
          countries: ["US"]
        }
      },
      access_token: req.body.access_token
    };

    const response = await axios.post(
      `https://graph.facebook.com/v2.12/act_${req.body.ADACCOUNT_ID}/adsets`,
      adSetData,
      {
        headers: {
          'Content-Type': 'application/json' // This is non-negotiable!
        }
      }
    );

    console.log("Ad Set created successfully:", response.data);
  } catch (err) {
    // Always check the API's error response for clues
    console.error("Error creating Ad Set:", err.response.data);
  }
}

Fix 2: Avoid Manual URL Encoding for Body Params

Unlike query strings, you don't need to URL-encode values like the targeting object in the request body. Just pass the raw JSON object—your HTTP client will handle encoding automatically when you set the correct Content-Type.

Bonus: Safer Access Token Placement

Instead of putting your access_token in the URL or body, you can add it to the request header for better security:

headers: {
  'Content-Type': 'application/json',
  'Authorization': `Bearer ${req.body.access_token}`
}

Then you can remove the access_token key from the request body entirely.

Debugging Tips

If you're still stuck:

  • Test the request first with Postman or curl to rule out code-specific issues. Here's a curl example:
    curl -X POST \
      https://graph.facebook.com/v2.12/act_<YOUR_AD_ACCOUNT_ID>/adsets \
      -H 'Content-Type: application/json' \
      -d '{
        "name": "My Reach Ad Set",
        "optimization_goal": "REACH",
        "billing_event": "IMPRESSIONS",
        "bid_amount": 2,
        "daily_budget": 1000,
        "campaign_id": "<YOUR_CAMPAIGN_ID>",
        "targeting": {"geo_locations": {"countries": ["US"]}},
        "access_token": "<YOUR_ACCESS_TOKEN>"
      }'
    
  • Check the API's error response carefully. Facebook returns detailed messages (e.g., missing parameters, invalid formatting) that will point you to the exact issue.

内容的提问来源于stack exchange,提问作者artgb

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 03:27:30