0

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 :)

1 Answer 1

0

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.