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

发送REST POST请求时出现<Response [502]>与TypeError错误的解决方法咨询

Fixing "TypeError: string indices must be integers" in REST POST Request

Got it, let's break down why you're hitting that 502 error with the TypeError: string indices must be integers message, and fix it from both the request sender and backend sides.

Why This Error Happens

The backend error tells us it’s trying to access request['param1'] like a dictionary/JSON object, but it’s actually getting a raw string instead. That’s because either your request isn’t sending valid JSON, or the backend isn’t parsing the request body correctly as JSON.

Your current code uses str.format() to build the JSON string, which is error-prone (it doesn’t handle special characters or proper JSON escaping). Here are two better approaches:

Option A: Use requests Built-in json Parameter

This is the simplest and most reliable way—requests handles JSON serialization and sets the correct Content-Type header automatically:

import requests

param1 = "abc"
param2 = "def"
input_data = {"param1": param1, "param2": param2}

# Pass the dictionary directly via the json parameter
resp = requests.post(scoring_uri, json=input_data)
print(resp)

Option B: Manually Serialize with json.dumps()

If you need more control over serialization, use json.dumps() instead of str.format() to ensure valid JSON:

import requests
import json

param1 = "abc"
param2 = "def"
# json.dumps properly escapes strings and generates valid JSON syntax
input_data = json.dumps({"param1": param1, "param2": param2})
headers = {'Content-Type': 'application/json'}

resp = requests.post(scoring_uri, data=input_data, headers=headers)
print(resp)

Fix 2: Adjust the Backend

If you can modify the backend code, make sure it’s parsing the incoming request body as JSON instead of treating it as a raw string. Here are examples for common frameworks:

For Flask:

Use request.get_json() to parse the JSON body into a dictionary:

from flask import Flask, request

app = Flask(__name__)

@app.route('/your-scoring-endpoint', methods=['POST'])
def process_scoring():
    # Parse the JSON request body
    request_data = request.get_json()
    # Now safely access parameters like a dictionary
    param1 = request_data['param1']
    param2 = request_data['param2']
    
    # Your business logic here
    return "Request processed successfully"

For Django:

Parse the raw request body with json.loads():

import json
from django.http import JsonResponse

def scoring_view(request):
    if request.method == 'POST':
        # Parse JSON from the request body
        request_data = json.loads(request.body)
        param1 = request_data['param1']
        param2 = request_data['param2']
        
        # Your logic here
        return JsonResponse({"status": "success"})

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.29 16:47:28