【发布时间】:2019-05-28 11:27:03
【问题描述】:
我正在尝试向服务器发送一个带有一些 json 数据的 python http 请求(我将在后面使用这个 json 数据)。服务器应该给出一些 json 数据的响应(使用 PHP)。不幸的是,即使请求状态代码是 200,也没有任何响应。请帮助!
#request.py
import requests
import urllib3
import json
import os
import time
import sys
#http Request
url = 'http://localhost/response.php'
headers = {'Content-type': 'application/json', 'Accept': 'application/json'}
while True:
postMessage = '{"Info": {"ID": 1, "IP": "192.168.1.1"}}'
response = requests.post(url, json=postMessage, headers=headers)
#trying to decode the response
try:
decodedresponse = json.loads(response.text)
print(decodedresponse)
except(ValueError, KeyError, TypeError):
print("some Error")
#always getting the above print statement!
break
#response.php
<?php
if(!empty($_POST['json'])){
$data = [ 'a', 'b', 'c' ];
header('Content-type:application/json;charset=utf-8');
echo json_encode($data);
}
?>
【问题讨论】:
-
postMessage = {"Info": {"ID": 1, "IP": "192.168.1.1"}} -
@OlvinRoght 还是同样的问题
-
因为您正在尝试查找不存在的帖子字段
json。 -
$data = file_get_contents("php://input");然后echo json_decode($data); -
@OlvinRoght 我遇到了同样的错误!
标签: php python http httprequest httpresponse