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

Here is the php code:

For convienence:

$url = "";

$data = array(
    "method" => "listSubscribe", 
    "params" => [
        "api_key" => "98844b0f8da95e7c8ace0aaa6234e4e2",
        "list_id" => "12345",
        "emails" => [

$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": [""]

r =, 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:

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: