如何在 Django 中提交 302 表单后发送 POST 请求?
How to send POST request after form submit 302 in Django?
我有一个实现如下:
有一个付款表格,其中用户填写了所有详细信息。(API1),这里我收到错误 302:
在提交该表单时调用了我认为的函数之一。
在后端实现中,即。在 views.py
中,我想向我集成的其中一个网关发送 POST 请求。(API2)
但是随着请求以 GET 方式进行,问题来了,因此它丢弃了我随请求一起发送的所有表单数据。
代码如下
views.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 请求连同所有参数一起发送?我在这里做错了什么?
感谢您的建议。
# here you are assigning the post url to content ie. 'https://www.billdesk.com/pgidsk/PGIMerchantRequestHandler?hidRequestId=****&hidOperation=****'
content = response.url
return_config = {
"type": "content",
"content": redirect(content) # calling redirect with the response.url
}
更改为:
# check status code for response
print(response)
content = response.json() # if response is of json in format
content = response.text # if response is of plain text
return_config = {
"type": "content",
"content": content
}
return return_config
request.post()
returns requests.Response
对象。为了获得响应数据,您需要使用 .text
或 .json()
访问它,具体取决于发送响应的格式。
如果请求 returns 302 状态,则新 url 在 response.headers['Location']
中可用。您可以继续关注新的url,直到您得到一个有效的回复。
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
我有一个实现如下:
有一个付款表格,其中用户填写了所有详细信息。(API1),这里我收到错误 302:
在提交该表单时调用了我认为的函数之一。
在后端实现中,即。在
views.py
中,我想向我集成的其中一个网关发送 POST 请求。(API2)
但是随着请求以 GET 方式进行,问题来了,因此它丢弃了我随请求一起发送的所有表单数据。
代码如下
views.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 请求连同所有参数一起发送?我在这里做错了什么?
感谢您的建议。
# here you are assigning the post url to content ie. 'https://www.billdesk.com/pgidsk/PGIMerchantRequestHandler?hidRequestId=****&hidOperation=****'
content = response.url
return_config = {
"type": "content",
"content": redirect(content) # calling redirect with the response.url
}
更改为:
# check status code for response
print(response)
content = response.json() # if response is of json in format
content = response.text # if response is of plain text
return_config = {
"type": "content",
"content": content
}
return return_config
request.post()
returns requests.Response
对象。为了获得响应数据,您需要使用 .text
或 .json()
访问它,具体取决于发送响应的格式。
如果请求 returns 302 状态,则新 url 在 response.headers['Location']
中可用。您可以继续关注新的url,直到您得到一个有效的回复。
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