gpt4 book ai didi

python-3.x - 如何在 Django 中提交 302 表单后发送 POST 请求?

转载 作者:行者123 更新时间:2023-12-04 09:25:58 25 4
gpt4 key购买 nike

我有一个实现如下:

  • 有一个付款表格,用户填写所有详细信息。( API1 ),在这里我收到错误 302:
    enter image description here
  • 在提交该表单时,我的 View 中的一个函数被调用。
  • 在后端实现即。在 views.py , 我想向我集成的网关之一发送 POST 请求。( API2 )
    enter image description here

  • 但是问题来了,因为请求是 GET,因此它丢弃了我与请求一起发送的所有表单数据。
    以下是代码。
    View .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'] 中可用。 .您可以继续关注新的 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

    关于python-3.x - 如何在 Django 中提交 302 表单后发送 POST 请求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63008387/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com