1

I have a RESTful API I'm requesting from with query string parameters of date_start and date_end. The API documentation states the request should have the date time in a sort of ISO string, like this: 2019-06-15T00:22:55. Setting a date range would look like this:

https://path/to/api?start_date=2019-06-15T00:22:55&end_date=2019-08-05T19:55:33

Python requests, though, mangles it by wanting to escape the colons (:), replacing them with %3A, resulting in

https://path/to/api?start_date=2019-06-15T00%3A22%3A55&end_date=2019-08-05T19%3A55%3A33

Which, unfortunately, the API doesn't regard as a soluble parameter.

When I run the URL from Postman, that formatting doesn't get mangled. However, attempting to connect my app to the API using the typically amazing Requests library, it does the string escape conversion. I'm passing the parameter in a payload-style key-value pair, advised by the requests docs, as follows:

info_id = 'abcXYZ1234'
url = 'path/to/sandbox'
start = '2019-06-15T00:00:00'
end = '2019-07-15T00:00:00'
payload = {'info_id': info_id, 'start_date': start, 'end_date': end}
r = requests.get(url, params=payload)
info = r.json()

How can I prevent requests from escaping colons?

Jon Mitten
  • 1,965
  • 4
  • 25
  • 53
  • did you try to use this value directly in url - `requests.get('https://path/to/api?start_date=2019-06-15T00:22:55&end_date=2019-08-05T19:55:33)` ? – furas Aug 05 '19 at 20:19
  • Good question - no, I use it in a param key-value pair. I'll update. – Jon Mitten Aug 05 '19 at 20:22
  • should also works when you assign as string or bytes to params - as in @Saritus answer. – furas Aug 05 '19 at 20:23
  • Possible duplicate of [How to prevent python requests from percent encoding my URLs?](https://stackoverflow.com/questions/23496750/how-to-prevent-python-requests-from-percent-encoding-my-urls) – Dainis Aug 05 '19 at 20:32

1 Answers1

4

You can prevent an encoding of the url by using the params parameter of requests.get

url = 'https://path/to/api'
r = requests.get(url, params='start_date=2019-06-15T00:22:55&end_date=2019-08-05T19:55:33')

Converting from the typical payload dictionary structure is quite easy (code from https://stackoverflow.com/a/23497912/10833207)

payload = {
  'start_date': '2019-06-15T00:22:55',
  'end_date': '2019-08-05T19:55:33'
}

payload_str = "&".join("%s=%s" % (k,v) for k,v in payload.items())

r = requests.get(url, params=payload_str)
Saritus
  • 918
  • 7
  • 15