Skip to content Skip to sidebar Skip to footer

Passing Parameters Without Escaping In Python3 Requests

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

Solution 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, Requestrequest= 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.

Post a Comment for "Passing Parameters Without Escaping In Python3 Requests"