3

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.

0

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Hi Martijn, looks like you are right, the problem stems deeper than a simple fix. Should I mark your response as correct even though my problem is not fixed? Or should I just leave it, I'm not sure what the stack etiquette is for this kind of thing :)
The checkmark means you found a post the most helpful. Wether or not you found my post helpful is entirely up to you! It doesn't really have to mean 'problem solved'; your question has been solved, of sorts.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.