python-3.x - 如何在 Django 中提交 302 表单后发送 POST 请求?
问题描述
我有一个实现如下:
有一个付款表格,用户填写所有详细信息。(API1),这里我收到错误 302:
在提交该表单时,会调用我认为的功能之一。
在后端实现中,即。在
views.py
,我想向我集成的网关之一发送一个 POST 请求。(API2)
但是问题来了,因为请求是以 GET 方式进行的,因此它正在丢弃我与请求一起发送的所有表单数据。
以下是代码。
视图.py -->
headers = {
'Content-Type': 'application/x-www-form-urlencoded',
}
payload = {
'CustomerID': 'abc',
'TxnAmount': '1.00',
'BankID': '1',
'AdditionalInfo1': '999999999',
'AdditionalInfo2': 'test@test.test',
}
payload_encoded = urlencode(payload, quote_via=quote_plus)
response = requests.post('https://www.billdesk.com/pgidsk/PGIMerchantRequestHandler?hidRequestId=****&hidOperation=****', data=payload_encoded, headers=headers)
content = response.url
return_config = {
"type": "content",
"content": redirect(content)
}
return return_config
如何将第二个请求(API2)作为 POST 请求连同所有参数一起发送?我在这里做错了什么?
谢谢你的建议。
解决方案
如果请求返回 302 状态,则新的 url 在response.headers['Location']
. 您可以继续关注新的网址,直到您得到有效的回复。
headers = {
'Content-Type': 'application/x-www-form-urlencoded',
}
payload = {
'CustomerID': 'abc',
'TxnAmount': '1.00',
'BankID': '1',
'AdditionalInfo1': '999999999',
'AdditionalInfo2': 'test@test.test',
}
payload_encoded = urlencode(payload, quote_via=quote_plus)
response = requests.post('https://www.billdesk.com/pgidsk/PGIMerchantRequestHandler?hidRequestId=****&hidOperation=****', data=payload_encoded, headers=headers)
while response.status_code == 302:
response = requests.post(response.headers['Location'], data=payload_encoded, headers=headers)
content = response.text
return_config = {
"type": "content",
"content": content
}
return return_config
推荐阅读
- linux - 未调用 Linux 自定义设备驱动程序探测和初始化函数
- python - 从命令行运行时出现 Python3 ModuleNotFoundError 但如果我进入 shell 则有效
- python - Python:取两个数据帧的最大值来创建第三个数据帧
- kubernetes - Kubernetes 入口和节点故障
- r - 删除或替换混合数据类型中的数值
- gridjs - 以编程方式搜索 gridjs 表(使用 javascript)
- spring-boot - 什么时候在测试中强制使用 TestEntityManager?
- graphql - 赛普拉斯等待 graphql 查询完成
- quarkus - Quarkus - 理解错误请求失败:java.lang.IllegalStateException:SRMSG00028:对事件的订阅已被取消
- javascript - 在反应中删除列表项保留较旧的项目