psf / requests

A simple, yet elegant, HTTP library.
https://requests.readthedocs.io/en/latest/
Apache License 2.0
52.19k stars 9.33k forks source link

Error when headers return 'Location' as an app://xxxx although I have set disable redirects #6758

Closed KenKout closed 4 months ago

KenKout commented 4 months ago

Expected Result

response['Location'] = app://xxxx

Actual Result

hostIP = socket.gethostbyname(hostonly) UnicodeError: encoding with 'idna' codec failed (UnicodeError: label too long)

Reproduction Steps

import requests
resp = requests.get('https://example', allow_redirects=False)
nateprewitt commented 4 months ago

Hi @KenKout, can you please supply the full set of information requested in the ticket template and the complete stacktrace?

What's provided currently is not reproducible.

This works as expected:

import requests
r = requests.get('https://httpbin.org/redirect-to?url=app://test', allow_redirects=False)

From the error, it looks like the location is being parsed at some point, it's not clear where that's happening.

KenKout commented 4 months ago
import requests
from requests.structures import CaseInsensitiveDict

url = "https://sandbox.moodledemo.net/login/index.php"
s = requests.Session()
headers = CaseInsensitiveDict()
headers["Content-Type"] = "application/x-www-form-urlencoded"
headers["Origin"] = "https://sandbox.moodledemo.net"
headers["Referer"] = "https://sandbox.moodledemo.net/login/index.php"
headers["User-Agent"] = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36 Edg/126.0.0.0"

resp = s.get(url, headers=headers)
logintoken = resp.text.split('logintoken" value="')[1].split('"')[0]
data = f"anchor=&logintoken={logintoken}&username=admin&password=sandbox"
resp = s.post(url, headers=headers, data=data)
print(resp.text)

url = 'https://sandbox.moodledemo.net/admin/tool/mobile/launch.php?service=moodle_mobile_app&passport=1&urlscheme=moodlemobile'
resp = s.get(url, headers=headers)
print(resp.text)

Hi, this is the script, sorry for late response It works well when I use library httpx

import requests
from requests.structures import CaseInsensitiveDict

url = "https://sandbox.moodledemo.net/login/index.php"
s = requests.Session()
headers = CaseInsensitiveDict()
headers["Content-Type"] = "application/x-www-form-urlencoded"
headers["Origin"] = "https://sandbox.moodledemo.net"
headers["Referer"] = "https://sandbox.moodledemo.net/login/index.php"
headers["User-Agent"] = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36 Edg/126.0.0.0"

resp = s.get(url, headers=headers)
logintoken = resp.text.split('logintoken" value="')[1].split('"')[0]
data = f"anchor=&logintoken={logintoken}&username=admin&password=sandbox"
resp = s.post(url, headers=headers, data=data)
print(resp.text)

import httpx
url = 'https://sandbox.moodledemo.net/admin/tool/mobile/launch.php?service=moodle_mobile_app&passport=1&urlscheme=moodlemobile'
resp = httpx.get(url, headers=headers, cookies=s.cookies)
print(resp.headers)

In case you can't try to recreate the error, it's because moodle sandbox resets every hour So when you want to try it Moodle Setting (username: admin; password: sandbox) Come to this page and change Type of login into this

image
sigmavirus24 commented 4 months ago

@KenKout you're not setting allow_redirects in your example and you still haven't provided any of the information in the issue template that's required.