du6333137 2014-02-04 22:58
浏览 85

使用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?

  • 写回答

1条回答 默认 最新

  • dtx9931 2014-02-04 23:01
    关注

    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"));
    
    评论

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?