2

I have an implementation as follows:

  1. There is a payment form wherein user fills all the details.(API1), here I'm getting an error 302:

    enter image description here

  2. On submit of that form one of the function in my views is called.

  3. In the backend implementation ie. in views.py, I want to send a POST request to one of the gateways I have integrated.(API2)

    enter image description here

But the problem is coming as the request is going as GET and hence it is dropping all the form data I'm sending along with the request.

Following is the code.

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

How do I send the 2nd request(API2) as POST request along with all parameters? What am I doing wrong here?

Thank you for your suggestions.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
mach2
  • 450
  • 2
  • 6
  • 24
  • 1
    why can't you do it in POST method instead of doing it in the redirected GET method of the view. – Vishal Singh Jul 23 '20 at 10:18
  • 1
    Why do you return a redirect at all? You have results from that external service after request and you can generate response with that results. You need to look into why you get 302 status. – wowkin2 Jul 23 '20 at 10:44
  • sorry not clear on what are you trying to achieve – Vinay Jul 28 '20 at 10:33

2 Answers2

2

If the requests returns a 302 status, the new url is available in response.headers['Location']. You can keep following the new url, till you end up with a valid response.

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
jerrymouse
  • 16,964
  • 16
  • 76
  • 97
nicks101
  • 1,065
  • 11
  • 20
0
# 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
}

change to:

# 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 object. inorder to get the response data you need to access it using .text or .json() depending on the format in which the response is sent.

Achuth Varghese
  • 2,356
  • 1
  • 4
  • 18