Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python HTTP Request URL End Slash

I am using the requests module in Python and seem to be experiencing an issue with it adding a trailing slash after the URL. I have tried removing this using different methods (replace, regex) but it doesn't seem to make any difference. Some details have been replace for privacy reason obviously :)

url = 'http://example.com:8000'
payload = {}
payload['summary'] = "test"
payload['service'] = 10
payload['template'] = 'XXX11112222'
payload['user_name'] = 'john_smith'
payload['justification'] = 'required'
payload['start_date'] = '1417737600'
payload['end_date'] = '1417759200'
r = requests.post(url, params=payload)
print(r.url)

The URL printed comes out as this.

http://X.X.X.X:8000/?end_date=1417759200&business_justification=required&service=10&summary=test&template=XXX11112222&user_name=john_smith&start_date=1417737600

And gives me this response.

200
400 No requested_service or service Parameter Provided

When I grab that URL and put it in an browser and remove the '/' before the '?' it works fine. Eg: http://example.com:8000?.... works, however http://example.com:8000/?... does not. But I can't work out how to remove the trailing slash off the URL in the Python script.

like image 945
dlyxzen Avatar asked Jul 13 '26 03:07

dlyxzen


1 Answers

Your server is at fault here, not the requests library.

The HTTP RFC states, in section 3.2.2:

If the abs_path is not present in the URL, it MUST be given as "/" when used as a Request-URI for a resource

A URL without a path (such as http://X.X.X.X:8000) has no absolute path component, so it is set to / as per the standard. If your server responds differently when no path is given, then it is violating that standard.

There is no work-around for this, short of patching the requests source code to remove the default value for the path portion of a URL. This'll have to be done in other locations too; I didn't do an exhaustive search.

like image 69
Martijn Pieters Avatar answered Jul 14 '26 19:07

Martijn Pieters