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

如何用Python Requests将字典作为URL查询参数调用第三方API

How to Pass a JSON Dictionary as a Query Parameter for Third-Party API Calls

Got it, let's walk through exactly how to pull this off based on your requirements and the example URL you shared. You need to send your JSON data as the value of a single query parameter named JsonData, properly URL-encoded to match the format in your example.

Step 1: Prepare Your JSON Data

First, define your transaction data as a standard Python dictionary:

import requests
import json
from urllib.parse import quote_plus

# Your transaction data in dictionary form
txn_data = {
    "MID": "MID117185435",
    "ORDERID": "ORDR4o22310421111",
    "CHECKSUMHASH": "NU9wPEWxmbOTFL2/UKr3lk6fScfnLy8wORc3YRylsyEsr2MLRPn/3DRePtFEK55ZcfdTj7mY9vS2qh+sm7oTRx/x4BDlvZBj/8Sxw6s/9io="
}

Step 2: Convert Dictionary to JSON String

Turn your dictionary into a valid JSON string. If the third-party API expects a compact, whitespace-free JSON (like your example), use the separators argument to minimize extra spaces:

# Compact JSON string (matches the condensed format in your example URL)
json_str = json.dumps(txn_data, separators=(',', ':'))

# Use the default if extra whitespace is allowed by the API:
# json_str = json.dumps(txn_data)

Step 3: URL-Encode the JSON String

This is critical—your example shows special characters (like /, +) are URL-encoded. Use quote_plus() to convert the JSON string into a URL-safe format that won't break the request:

encoded_json = quote_plus(json_str)

Step 4: Make the API Request

You have two reliable ways to send this with requests:

Option 1: Build the URL Manually

If you want to see the exact URL string (matching your example), construct it directly:

base_url = "https://pguat.paytm.com/oltp/HANDLER_INTERNAL/getTxnStatus"
full_url = f"{base_url}?JsonData={encoded_json}"

response = requests.get(full_url)

Option 2: Use requests Params (Recommended)

Let requests handle the URL structure for you—this is cleaner and less error-prone:

params = {"JsonData": encoded_json}
response = requests.get("https://pguat.paytm.com/oltp/HANDLER_INTERNAL/getTxnStatus", params=params)

Key Notes to Avoid Mistakes

  • Don’t pass the raw dictionary to params: If you do, requests will split the dictionary into multiple query parameters (e.g., MID=xxx&ORDERID=xxx), which is not what you need. We need the entire JSON as a single value for JsonData.
  • Check API Requirements: Double-check if the third-party expects specific formatting (like strict key casing, no spaces) to avoid validation errors.
  • Verify Encoding: After encoding, your encoded_json should mirror the value in your example URL—special characters will be replaced with their URL-encoded equivalents (e.g., / becomes %2F, + becomes %2B).

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.20 11:32:48