supersuraccoon supersuraccoon - 24 days ago 19
Python Question

python HTTPSConnection curl -g parameter

I am try to translate a curl command in python using HTTPSConnection.

The origin curl command is :

curl -X DELETE \
-H "X-LC-Id: "id" \
-H "X-LC-Key: "key" \
-G \
--data-urlencode 'limit=10' \
https://xxx/1.1/logs


The following is a working solution :

connection = httplib.HTTPSConnection("https://xxx")
connection.connect()
connection.request(
"GET",
"/1.1/logs?limit=" + 10,
json.dumps({}),
{
"X-LC-Id" : "id",
"X-LC-Key" : "key",
}
)
results = json.loads(connection.getresponse().read())
return results


This works fine with 10 results returned.

But, the following do not work:

connection = httplib.HTTPSConnection("https://xxx")
connection.connect()
connection.request(
"GET",
"/1.1/logs",
json.dumps({"limit": "10"}),
{
"X-LC-Id" : "id",
"X-LC-Key" : "key",
}
)
results = json.loads(connection.getresponse().read())
return results


This solution returns all the messages from the server instead of 10.

Where should I put those parameters in curl -g filed when used in HTTPSConnection without having to make the request string like :

"/1.1/logs?limit=" + 10 + "&aaa=" + aaa + "&bbb=" + bbb + ...


Any advice is appreciated, thanks :)

Answer

The third parameter to request is the query body, which should not be sent for a GET request. (It seems the service is not reading the body, which is why the limit is not respected.) You will need to append the query string, but you might want to look into generating it from a Python dict instead of rolling it by hand.

Comments