I'm trying to filter data from an API request to get only registers that were created or updated yesterday.
There are some notes listed in the documentation, some are shown below.
2. The query must be URL encoded
4. Query string must be enclosed between a pair of double quotes and can have up to 512 characters
5. Logical operators AND, OR along with parentheses () can be used to group conditions
6. Relational operators greater than or equal to :> and less than or equal to :< can be used along with date fields and numeric fields
The format of the request is: .../api/v2/search/tickets?query="created_at:>'2017-01-01' OR updated_at:>'2017-01-01'"
Url encoded it becomes: .../api/v2/search/tickets?query="created_at:>%272017-01-01%27%20OR%20updated_at:>%272017-01-01%27"
I wrote the following code:
import datetime as dt
import urllib.parse
import requests
url = f".../api/v2/search/tickets"
params={}
yesterday = dt.date.today() - dt.timedelta(days = 1)
query = urllib.parse.quote(f"created_at:>{yesterday} OR updated_at:>{yesterday}")
params["query"] = query
response = requests.get(url, auth=(api_key, "X"), params=params)
I'm using urllib.parse.quote to URL encode the string but I'm getting a different format if compared to the example provided from the API documentation.
.../tickets?query=created_at%253A%253E%25272022-10-30%2527%2520OR%2520updated_at%253A%253E%25272022-10-30%2527
Error raised: requests.exceptions.HTTPError: 400 Client Error: Bad Request for url
I can't find a way to keep the double quotes, I tried to enclose the double quotes in single quotes but it doesn't work.
I can't find a way to encode the filter string as the example shown in the API Documentation.
Any help?
requestsis perfectly capable of URL-encoding your data. That's what has happened here -- it's getting double-encoded. Note the %25, which is a % sign.>is a character that gets translated during URL encoding. There should not be>signs in your final URL.