0

problem:

  1. How to translate curl_setopt_array from PHP to Python
  2. 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:

  1. 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!"}
  1. 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!"}
Fian Julio
  • 39
  • 6
  • Don't post your api keys here. they will be missused – Bendik Knapstad Jul 13 '22 at 06:13
  • ok thanks, I changed the API keys – Fian Julio Jul 13 '22 at 06:20
  • try sending the data as headers maybe? is there any form of documentation for this api? – Bendik Knapstad Jul 13 '22 at 06:26
  • I don't have any documentation for this API, still, I got this response if I use to send the data as headers {"status":false,"msg":"Incorrect parameters!"} – Fian Julio Jul 13 '22 at 06:35
  • 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) – CBroe Jul 13 '22 at 06:43
  • still the same i got {"status":false,"msg":"Incorrect parameters!"}, i was trying that solution with this code : 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) – Fian Julio Jul 13 '22 at 07:43

0 Answers0