1

I have this curl request I would like to convert to python 3

curl -X "POST" "https://conversations.messagebird.com/v1/send" \\
-H "Authorization: AccessKey YOUR-API-KEY" \\
-H "Content-Type: application/json" \\
--data '{ "to":"+31XXXXXXXXX", "from":"WHATSAPP-CHANNEL-ID", "type":"text", "content":{ "text":"Hello!" }, "reportUrl":"https://example.com/reports" }'

can anyone help me please?

I've tried the following request, but not working :

import requests

header = {"Authorization":"AccessKey YOUR-API-KEY"}
data = { "to":"+31XXXXXXXXX", "from":"WHATSAPP-CHANNEL-ID", "type":"text", "content":{"text":"Hello!" },  "reportUrl":"https://example.com/reports"}
url = 'https://conversations.messagebird.com/v1/send'
response = requests.post(url, data=data, headers=header)
print(response.text)

I'm having the error message :

<Response [400]>
{"errors":[{"code":21,"description":"JSON is not a valid format"}]}
Maurice D.
  • 11
  • 2
  • Does this answer your question? [How to POST JSON data with Python Requests?](https://stackoverflow.com/questions/9733638/how-to-post-json-data-with-python-requests) – Ajay Jan 28 '21 at 02:09

2 Answers2

0
  1. you can use json instead of data,
requests.post('http://httpbin.org/post', json={"key": "value"})
  1. you need dump data
requests.post(url, data=json.dumps(data), headers=headers)
  1. thers is another question like yours.
KennetsuR
  • 704
  • 8
  • 17
0

According to their documentation, the cURL is correct and would be implemented as is in Python:

import requests

header = {
    "Authorization": "AccessKey YOUR-API-KEY", 
    "Content-Type": "application/json"
}
data = {
    "to": "+31XXXXXXXXX", 
    "from": "WHATSAPP-CHANNEL-ID", 
    "type": "text", 
    "content": {"text":"Hello!"}, 
    "reportUrl": "https://example.com/reports"
}
url = 'https://conversations.messagebird.com/v1/send'
response = requests.post(url, json=data, headers=header)

print(response.json())
Gab
  • 3,404
  • 1
  • 11
  • 22