0

this is my first ever question on here.

Currently I am looking into the Python requests module. I am trying to automate a task for which i need to pass on a csrf token. The csrf token can be found in the payload of a previous request.

How can I extract the value out of the Payload?

Example Payload:

value1: ABCD

value2: EFGH

csrf_token: the token I am looking for

value3: false

Sorry if this is a dumb or easy question but I am not able to solve it right now.

Thanks for your help!

  • Does this answer your question? [Passing csrftoken with python Requests](https://stackoverflow.com/questions/13567507/passing-csrftoken-with-python-requests) – RoseGod Nov 21 '21 at 20:38
  • @RoseGod I dont think so. In the webpages content I can find 2 csrf tokens which do not have the same value and I think I need to choose the one from the payload. Or can I just use one of the ones in the content? – nousername46291772 Nov 21 '21 at 20:51
  • What is that example payload _really?_ You're just showing some text. Is it in JSON format? Don't try to interpret for us; show us the real details. The less you change, the better. See [ask]. – ChrisGPT was on strike Nov 24 '21 at 23:22

1 Answers1

1

Here are some examples of where the data you might be looking for is located.

import requests


response = requests.get('http://some_url')
# raise an exception if the status  code != 200
response.raise_for_status()

# the contents of the response. (bytes)
contents = response.contents

# the contents of the response if the contents are 
# formatted as JSON. (dictionary)
contents = response.json()

# the headers of the response. (dictionary)
headers = response.headers

# You also have to consider if you are using the correct HTTP protocol
payload = {}
response = requests.put('http://some_url', json=payload)
response = requests.post('http://some_url', json=payload)

# the responses are going to function just like when using the "get" protocol
  • First of all, Thank you! In the meantime I looked around and extracted the csrf token from the pages content but it does not work. It gives me the error b'{"error":"CSRF Token mismatch"}' As I said above, I think the right csrf token is located in the requests Form Data (Payload). Is there a way to extract it from there? – nousername46291772 Nov 21 '21 at 21:16
  • I would need to see exactly what you are trying to grab. You are mentioning "form" data when before it was "payload" data. which may be a post and not a get. and maybe the data you are looking for in encapsulated in the response to the post. –  Nov 21 '21 at 22:36
  • I am sorry, I made a mistake in my code which i found by now. Was able to find the token now in the html. Thank you for answering me and trying to help :) – nousername46291772 Nov 22 '21 at 00:20