Click here to Skip to main content
15,742,477 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Can someone help me to fix the error. When sending

Here are the sample codes:

s = requests.Session()
response = s.get("")
csrf_token ='"csrf_token":"(.*?)"', response.text)[1]
s.headers.update({"X-CSRFToken": csrf_token})

What I have tried:

I made these codes now i am stuck
Updated 1-May-21 23:32pm
Richard MacCutchan 2-May-21 4:01am    
"help me to fix the error"
We cannot guess what the error is. Please provide proper details of your problem.

1 solution

What's the real error message? I can guess that it's a Type Error, but one thing is certain: There is no syntax error in that sample you posted. The Type Error what you'll get if that call doesn't find a match. The result on no match is None and you get a TypeError exception when you try to index that with [] brackets.

Also, your regular expression "csrf_token":"(.*?)" is suspect, since a ? (indicating 0 or 1 occurrences of the previous item) is meaningless after an * (meaning 0 or more occurrences). If it does match anything, it will match all of the .text field starting from the first occurrence of ''"csrf_token":"(.*?)"'. That can't be what you want. (If it was, you didn't need a regular expression.)

After you fix your regular expression, you can sensibly avoid the type error with something like:
m ="'"csrf_token":"([^"]*)"', response.text)
if m != None:
    s.headers.update({'X-CSRFToken' :;

That has two more guesses in it: First, that you want the match to include just one ("") quoted field immediately after a "csrf_token": match. That's why I used '[^"]' to match any character EXCEPT a quote, instead of '.'. Then, I guessed that you only wanted the quoted text to be included in your HTTP header. I don't do enough web work to know if that makes sense or not. Someone else might be able to help with that if you posted exactly what you want to accomplish.
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900