-1

I'm trying to code the following cURL API request in Python:

curl -X POST 'https://api.livecoinwatch.com/coins/list' \
  -H 'content-type: application/json' \
  -H 'x-api-key: <YOUR_API_KEY>' \
  -d '{"currency":"USD","sort":"rank","order":"ascending","offset":0,"limit":2,"meta":false}'

I tried solving it with guidance of another post, like this:

headers = {
    'x-api-key': <YOUR_API_KEY>,
    'content-type': 'application/json',
    'host': https://api.livecoinwatch.com/coins/list
}

url = https://api.livecoinwatch.com/coins/list
data = '{"currency": "USD","sort": "rank","order": "ascending","offset": 0,"limit": 50,"meta": true}'

response = requests.post(url, data=json.dumps(data), headers=headers)

print (response)

Unfortunately I get a "bad request" error.

Can someone please help me where I go wrong?

  • What is considered a “bad request” is entirely up to the target server. Without any documentation on how the request is *expected* to look by the server this isn’t possible to answer. [ask] – esqew Nov 12 '21 at 20:53
  • Does this answer your question? [How to send POST request?](https://stackoverflow.com/questions/11322430/how-to-send-post-request) – aberkb Nov 12 '21 at 20:53
  • 1
    `'host': https://api.livecoinwatch.com/coins/list` should throw all sorts of errors, no? – esqew Nov 12 '21 at 20:54

1 Answers1

1

Assuming you have your urls wrapped in quotes, you should try giving to the data function parameter a dictionary instead of a string as the requests documentation says: data – (optional) Dictionary, list of tuples, bytes, or file-like object to send in the body of the Request.

response = requests.post(url, data=json.loads(data), headers=headers)