使用Python的POST请求的json数据没有到达

I like to build a small REST Interface to connect PYTHON with PHP. After some hours of google I ended with nearly copy&paste code from several discussion-boards:

import requests
import json

url = 'http://spidercontrol.ilumiweb.local/helge/voltage'

headers = {'Content-type': 'application/json', 'Accept': 'text/plain'}
data = {'value': '7.4', 'decay_time': '300'}

r = requests.post(url, data=json.dumps(data), headers=headers)

This should send the data as json via POST to the given url.This is where it's sended to:

<?php

  echo "Method: \t".$_SERVER['REQUEST_METHOD']."
";
  print_r(getallheaders());
  echo "Post:";
  print_r($_POST);
  echo "Get:";
  print_r($_GET);
  exit;
?>

But there is nothing in the $_POST data:

Method:         POST
Array
(
    [Host] => spidercontrol.ilumiweb.local
    [Content-Length] => 37
    [Content-type] => application/json
    [Accept-Encoding] => gzip, deflate, compress
    [Accept] => text/plain
    [User-Agent] => python-requests/2.2.1 CPython/2.7.6 Windows/7
)
Post:Array
(
)
Get:Array
(
)

If I use the same code, but remove the headers=headers information but data=data it works well. Did someone know why?

In PHP $_POST only contains form data (application/x-www-form-urlencoded or multipart/form-data encoded POST bodies).

To get JSON data you'll need to read the request body directly using php://input instead:

$json = json_decode(file_get_contents("php://input"));