3

I'm using the excellent requests package by Kenneth Retiz to make web requests. Requests Github repository

Sometimes I need to pass in parameters that are comma separated, but the commas are not url encoded, i.e. my url would look like this

http://example.site.com/customers?fields=id,first_name,last_name

I can't figure out how to format the query parameters to come out like that. The comma always gets escaped to %2C.

I've tried the following code, but that doesn't work:

params['fields'] = 'id,first_name,last_name'
url = 'http://example.site.com/customers'    
ex = requests.get(url, params = params)

Thanks.

2
  • FYI, comma is a "reserved character" in URLs, and should be encoded. The server decodes the parameter before use. Can you expand on why you want the comma to be passed through unescaped? Commented Jan 10, 2014 at 18:31
  • Consider this url: httpbin.org/get?fields=a%2cb%2cc. If you click though it, you'll see that the httpbin server receives an encoded URL, but correctly decodes the fields argument. Commented Jan 10, 2014 at 18:43

1 Answer 1

1

When you say that "my url would look like this: http://example.site.com/customers?fields=id,first_name,last_name", I assume that you mean that's what your URL would look like in your browser. The important thing about how browsers display the URL to you is that they will, to make it easier to read, unencode the URL for display purposes but when you visit that URL, they actually send the encoded version.

As Rob already mentioned in the comments on your question, there is no good reason to ever send the comma unencoded but that doesn't mean that Requests disallows this, it just makes it very difficult.


With the strong warning taken care of, there is a way to do what you want:

from requests import Session, Request
request = Request('GET', 'https://httpbin.org/get')
prepared = request.prepare()
prepared.url += '?field=id,first_name,last_name'
session = Session()
response = session.send(prepared)

This is highly error-prone because you're adding your own parameters without exactly checking anything but this will help you achieve what you're asking in the question. It side-steps requests doing the right thing for you and allows you to do what you want. I strongly suggest, however, that you read more into the advanced documentation for how to achieve everything else you might expect from the general requests API like streaming, certificate verification, etc.

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

Comments

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.