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

API POST响应仅读取DataFrame转列表最后一条记录及表单传参问题

Question 2: Convert DataFrame to application/x-www-form-urlencoded for ActiveCampaign API

First, let’s clarify: application/x-www-form-urlencoded is a format where data is sent as key-value pairs, just like URL query parameters (e.g., key1=value1&key2=value2). The good news is Python’s requests library handles this encoding automatically if you pass a dictionary to the data parameter of requests.post().

Here’s a step-by-step solution:

1. Install required dependencies

Make sure you have these packages installed:

pip install pandas requests

2. Code to send each contact from the DataFrame

import pandas as pd
import requests

# Replace these with your actual ActiveCampaign credentials
API_KEY = "your_activecampaign_api_key"
API_URL = "your_activecampaign_api_endpoint"  # Typically ends with /admin/api.php

# Recreate your sample DataFrame (you can skip this if you already have it)
data = {
    "email": ["belle@yandex.com", "apple765@yandex.com", "pear432@yandex.com"],
    "p[1]*": [1, 1, 1]
}
df = pd.DataFrame(data)

# Loop through each row to send a POST request per contact
for index, row in df.iterrows():
    # Build the payload with mandatory API parameters
    payload = {
        "api_key": API_KEY,
        "api_action": "contact_add",  # The specific API call you're making
        "api_output": "json",  # Choose json or xml for response format
        "email": row["email"],
        "p[1]": row["p[1]*"],  # Map your DataFrame column to the API's list ID parameter
        "status": 1  # Optional: 1 = active, 2 = unsubscribed (default is 1)
    }

    # Send the POST request
    response = requests.post(API_URL, data=payload)

    # Verify success and handle the response
    if response.status_code == 200:
        result = response.json()
        if result.get("success") == 1:
            print(f"Successfully added contact: {row['email']}")
        else:
            print(f"Failed to add {row['email']}: {result.get('error_message')}")
    else:
        print(f"Request failed for {row['email']} with status code: {response.status_code}")

Key details to note:

  • The api_action must be set to contact_add as specified in the API docs.
  • p[1] refers to the list ID (your data uses 1). The asterisk in your DataFrame column name is likely just a required field indicator, so we map it directly to p[1] in the payload.
  • To add contacts to multiple lists, include additional p[X] parameters (e.g., p[2] = 1 for list ID 2).

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.20 12:26:57