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

如何使用Python调用指定Curl请求获取数据?

How to replicate this cURL request in Python?

I want to use this cURL request:

curl 'https://cloud.estimote.com/v2/analytics/visits?from=1448928000&to=1451606399&granularity=daily' -X GET -u YOUR_SDK_APP_ID:YOUR_SDK_APP_TOKEN

The response format looks like this:

{
  "data": [
    { "date": "2016-02-22", "count": 183 },
    { "date": "2016-02-23", "count": 162 }
  ]
}

How can I call this cURL request using Python?


The requests library makes HTTP requests in Python extremely straightforward—this is the go-to method for most developers. Here's how to replicate your cURL call:

  1. First, install the library if you haven't already:

    pip install requests
    
  2. Then use this code snippet:

    import requests
    
    # Replace these with your actual credentials
    SDK_APP_ID = "YOUR_SDK_APP_ID"
    SDK_APP_TOKEN = "YOUR_SDK_APP_TOKEN"
    
    # The API endpoint and query parameters
    url = "https://cloud.estimote.com/v2/analytics/visits"
    params = {
        "from": 1448928000,
        "to": 1451606399,
        "granularity": "daily"
    }
    
    # Send the GET request with basic authentication (matches the -u flag in cURL)
    response = requests.get(url, params=params, auth=(SDK_APP_ID, SDK_APP_TOKEN))
    
    # Handle the response
    if response.status_code == 200:
        # Parse the JSON response into a Python dictionary
        data = response.json()
        print("Fetched visit data:")
        for entry in data["data"]:
            print(f"Date: {entry['date']}, Visit count: {entry['count']}")
    else:
        print(f"Request failed with status code {response.status_code}: {response.text}")
    

This code mirrors exactly what your cURL command does: it passes the query parameters cleanly, handles basic auth automatically, and makes parsing the JSON response trivial.


Using Python's built-in urllib library (No external dependencies)

If you can't install external libraries, you can use Python's standard urllib module. It's a bit more verbose, but works out of the box:

import urllib.request
import json
from base64 import b64encode

# Replace with your actual credentials
SDK_APP_ID = "YOUR_SDK_APP_ID"
SDK_APP_TOKEN = "YOUR_SDK_APP_TOKEN"

# Build the full URL with query parameters
url = "https://cloud.estimote.com/v2/analytics/visits?from=1448928000&to=1451606399&granularity=daily"

# Manually encode credentials for basic authentication (what cURL's -u does under the hood)
credentials = f"{SDK_APP_ID}:{SDK_APP_TOKEN}".encode()
encoded_auth = b64encode(credentials).decode("utf-8")
headers = {"Authorization": f"Basic {encoded_auth}"}

# Create and send the request
request = urllib.request.Request(url, headers=headers, method="GET")
try:
    with urllib.request.urlopen(request) as response:
        # Read and parse the JSON response
        response_data = json.loads(response.read().decode())
        print("Fetched visit data:")
        for entry in response_data["data"]:
            print(f"Date: {entry['date']}, Visit count: {entry['count']}")
except urllib.error.HTTPError as e:
    print(f"Request failed with status code {e.code}: {e.read().decode()}")

This approach handles the authentication header manually, so you don't need any third-party packages.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.22 08:18:18