Python请求值错误要解压缩的值太多

2024-04-26 07:43:12 发布

您现在位置:Python中文网/ 问答频道 /正文

虽然下面的数据是由邮递员发送的,带有必要的标题,但我得到了正确的响应,状态代码为200。但是当我使用相同的数据点击python请求中的函数时,我从这一行``中得到“error.ValueError太多的值需要解包”。 数据:

[{
     "phone_number": "data",
     "notification_type": "data",
     "is_success": true,
     "amount": 58,
     "account_no": "data"
}]
def serve_sms(self, data: dict):
   token = self.token(sms_service=True)
   header = {

       "service-name": "service",
       "service-key": settings.Service,
       "Authorization": "TOKEN " + token
   }
   try:
       requests.post(settings.SMS_SERVE_URL, data=data, headers=header)
   except Exception as e:
       core_log().debug("serve sms request error {}".format(e))

我尝试用json json=data更改响应内部的“数据”,但在那里我得到了404的状态代码

更新0:完整跟踪

http://... /api-service-auth/
DEBUG:urllib3.connectionpool:Starting new HTTP connection (1): ....:80
DEBUG:urllib3.connectionpool:http://...:80 "POST /api-service-auth/ HTTP/1.1" 200 212
DEBUG:connect_core:serve sms request error too many values to unpack (expected 2)

Tags: 数据代码coredebugselftokendatasettings
1条回答
网友
1楼 · 发布于 2024-04-26 07:43:12

看起来你的JSON格式是错误的。下面的代码为我工作

import requests 

result = requests.post("http://www.example.com", data={'list': [{
     "phone_number": "data",
     "notification_type": "data",
     "is_success": True,
     "amount": 58,
     "account_no": "data"
}]})

print(result)
C:\temp\python>python3 request.py
<Response [200]>

相关问题 更多 >