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

求助:将Curl GET请求转为Python 2.7.5 Requests代码及字典转内联JSON

Hey there! Let's get that curl command converted to Python 2.7.5's requests module sorted out. Since you didn't share the exact curl command you're running, I'll walk you through a general conversion workflow with a realistic example, plus cover the most common gotchas that trip people up when making this switch.


Step 1: Break down your curl command

First, let's map curl's flags to their requests equivalents—this makes conversion straightforward:

  • The core URL: Goes directly as the first argument to requests.get()
  • Headers (-H "Key: Value"): Pack these into a Python dictionary and pass via the headers parameter
  • Query parameters (either in the URL, or -d "param=val" for GET): Use the params dictionary to let requests handle encoding correctly
  • Basic auth (-u user:pass): Pass a tuple auth=("username", "password")
  • Cookies (-b "cookie=val"): Use the cookies dictionary parameter
Step 2: Example Conversion

Let's say your working curl command looks like this:

curl -X GET "https://api.your-service.com/endpoint" \
  -H "Authorization: Token YOUR_API_KEY" \
  -H "Accept: application/json" \
  -d "filter=active&per_page=20"

Here's how to translate that to Python 2.7.5-compatible code:
First, make sure you have the right version of requests installed (Python 2.7 only supports up to requests 2.27.1):

pip install requests==2.27.1

Then the Python code:

import requests

# Base API URL
url = "https://api.your-service.com/endpoint"

# Match the headers from your curl command
headers = {
    "Authorization": "Token YOUR_API_KEY",
    "Accept": "application/json"
}

# Query parameters from the `-d` flag (GET requests use these in the URL)
params = {
    "filter": "active",
    "per_page": 20
}

try:
    # Send the GET request
    response = requests.get(url, headers=headers, params=params)
    
    # Raise an error if the request failed (4xx/5xx status codes)
    response.raise_for_status()
    
    # Parse the JSON response (adjust if the API returns plain text/XML)
    response_data = response.json()
    print("Request succeeded! Data:", response_data)
except requests.exceptions.RequestException as e:
    # Catch all request-related errors (network issues, bad status codes, etc.)
    print("Request failed with error:", e)
    # Optional: Print the raw response content for debugging
    print("Raw response content:", response.content)
Common Pitfalls to Avoid
  • Python 2.7 String Encoding: If you're dealing with non-ASCII characters, use Unicode strings (u"your-string") to avoid encoding errors
  • SSL Verification: If your curl uses -k to skip SSL checks, add verify=False to requests.get() (note: this is insecure for production use!)
  • Body vs. Query Params: For GET requests, curl's -d adds query parameters, not request body data. Don't mix these up with the data parameter in requests (that's for POST bodies)
  • Cookie Handling: If your curl uses -b to send cookies, map them to a cookies dictionary like cookies={"session_id": "your-id"}

If you share your exact curl command, I can tweak this example to match your use case perfectly!


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.20 07:18:30