problem:
- How to translate curl_setopt_array from PHP to Python
- I want to use the webservice using python, I got this code in PHP
My environment:
- Python 3.9
- Windows 11
PHP code:
<?php
$data = [
'api_key' => 'APIKEY',
'sender' => '6285157853022',
'number' => '082136815088',
'message' => 'the message'
];
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "http://wa.mydomain.id/wa/app/api/send-message",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => json_encode($data))
);
$response = curl_exec($curl);
curl_close($curl);
echo $response;
?>
and I got success response
{"status":true,"msg":"Message sent successfully"}
I was trying this code in python:
import requests
import json
data = {
'api_key': 'APIKEY',
'sender': '6285157853022',
'number': '082136815088',
'message': 'the message'
}
response = requests.post('http://wa.mydomain.id/wa/app/api/send-message', data=data)
print(response.text)
and I got this response
{"status":false,"msg":"Incorrect parameters!"}
how to fix it? thanks for helping me.
tried solutions:
- I tried sending the data with headers but still doesn't work
The code:
import requests
url = "http://wa.mydomain.id/wa/app/api/send-message"
payload={}
files={}
headers = {
'api_key': 'APIKEY',
'sender': '6285157853022',
'number': '6282136815088',
'message': 'hi'
}
response = requests.request("POST", url, headers=headers, data=payload, files=files)
print(response.text)
Result:
{"status":false,"msg":"Incorrect parameters!"}
- I tried this solution (How to POST JSON data with Python Requests?) but still doesn't work
The code :
import requests
import json
data = {
'api_key': 'APIKEY',
'sender': '6285157853022',
'number': '082136815088',
'message': 'the message'
}
response = requests.post('http://wa.mydomain.id/wa/app/api/send-message', json=data)
print(response.text)
the result:
{"status":false,"msg":"Incorrect parameters!"}