Send GET request with body

I use elasticsearch, and the RESTful API supports reading bodies in GET requests for search criteria.

I'm currently doing

response = urllib.request.urlopen(url, data).read().decode("utf-8") 

If data present, it returns POST, otherwise GET. How can I force a GET even though I include the data (which should be in the body of the request according to POST)

Nb: I know that I can use the source property in Url, but the executed queries are complex and the definition of the query is verbose, which leads to extremely long queries.

+4
source share
1 answer

I do not know how to do this using urllib. However, requests makes it trivial (and actually trivial with any arbitrary verb and content request) using the request.request * function:

 requests.request(method='get', url='localhost/test', data='some data') 

Building a small test web server will show that the data is indeed sent in the body of the request and that the method perceived by the server is indeed GET.

* note that I am associated with the requests.api.requests code, because this is where the actual function definition lives. You should call it with requests.request(...)

+7
source

All Articles