You need to enable JavaScript to run this app. 如何将带JSON文件参数的Curl命令转换为可正常运行的Python脚本
优惠活动
大模型
产品
解决方案
定价
更多

如何将带JSON文件参数的Curl命令转换为可正常运行的Python脚本

正确Python脚本实现

推荐使用requests内置的json参数自动处理序列化,代码如下:

import requests
import json

headers = {
    'Authorization': 'Bearer 替换为你的实际Bearer Token',
}

# 读取json文件内容
with open('q1.json', 'r', encoding='utf-8') as f:
    json_content = json.load(f)

response = requests.post(
    'https://myeducationcompany.instructure.com/api/v1/courses/9391/quizzes/196242/questions',
    headers=headers,
    json=json_content
)

# 验证请求结果
print(response.status_code)
print(response.text)

如果要和curl行为完全对齐,手动读取文件内容传给data参数也可:

import requests

headers = {
    'Content-type': 'application/json',
    'Authorization': 'Bearer 替换为你的实际Bearer Token',
}

# 直接读取文件文本内容
with open('q1.json', 'r', encoding='utf-8') as f:
    data_content = f.read()

response = requests.post(
    'https://myeducationcompany.instructure.com/api/v1/courses/9391/quizzes/196242/questions',
    headers=headers,
    data=data_content
)
Postman请求配置步骤
  • 请求方法选择POST,填入请求URL:https://myeducationcompany.instructure.com/api/v1/courses/9391/quizzes/196242/questions
  • 切换到Headers标签页,添加2个请求头:
    • Key:Content-Type,Value:application/json
    • Key:Authorization,Value:Bearer 替换为你的实际Bearer Token
  • 切换到Body标签页,选择raw格式,右侧格式下拉菜单选择JSON,将q1.json的全部内容粘贴到输入框中
  • 点击Send按钮即可发起请求
错误原因

curl的-d @q1.json参数会读取q1.json文件的全部文本内容作为请求体发送,而原有Python代码直接将open('q1.json')返回的文件对象传给data参数,二者行为不一致,服务器接收到格式非法的请求体后直接断开连接,因此抛出RemoteDisconnected异常。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.15 11:15:00