How to convert http_build_query in PHP to a python equivalent for form data

Here is the php code:

https://help.sender.net/knowledgebase/id-like-to-use-the-api-is-there-anything-to-help-me-get-started/

For convienence:

$url = "https://app.sender.net/api/";

$data = array(
    "method" => "listSubscribe", 
    "params" => [
        "api_key" => "98844b0f8da95e7c8ace0aaa6234e4e2",
        "list_id" => "12345",
        "emails" => [
            "name.lastname@example.com"
        ]
    ]
);

$options = [
    'http' => [
        'method'  => 'POST',
        'content' => http_build_query(array('data' => json_encode($data)))
    ]
];

$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);

My attempt in python:

import requests
myobj = {
    "method": "listSubscribe", 
    "params": {
        "api_key": sender_api_key,
        "list_id": sender_list_id,
        "emails": ["email@email.com"]
    }
}


r = requests.post(sender_api_url, files={'data':json.dumps(myobj)})

I have tried so many combinations and I continue to get {"error":{"code":"002","message":"Empty request"}} as a response.

I am getting a 200 from r.response so it is at least being sent.

When I change my API key to something invalid I get the same error.



Read more here: https://stackoverflow.com/questions/64397149/how-to-convert-http-build-query-in-php-to-a-python-equivalent-for-form-data

Content Attribution

This content was originally published by billfreeman44 at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: