0

I am trying to send a python http request with some json data (I will use this json data in a later case) to server. Server should give a response with some json data (using PHP). Unfortunately there is no any response even though request status code is 200. Any help please!

#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);

}
 ?>

14
  • postMessage = {"Info": {"ID": 1, "IP": "192.168.1.1"}} Commented May 28, 2019 at 11:28
  • @OlvinRoght still same problem Commented May 28, 2019 at 11:29
  • Cause you're trying to find post field json which doesn't exist. Commented May 28, 2019 at 11:31
  • 1
    $data = file_get_contents("php://input"); and then echo json_decode($data); Commented May 28, 2019 at 11:32
  • @OlvinRoght I am getting the same error! Commented May 28, 2019 at 11:49

2 Answers 2

1

You should remove quotes on assignment of postMessage in your:

postMessage = {"Info": {"ID": 1, "IP": "192.168.1.1"}}

And change your php code to:

<?php
$data = file_get_contents("php://input");
header('Content-type:application/json;charset=utf-8');
echo json_encode($data);
?>
Sign up to request clarification or add additional context in comments.

Comments

0

Check your $_POST structure.

<?php
if(!empty($_POST['Info'])){
   $data = [ 'a', 'b', 'c' ];  
   echo json_encode($data);
 }
 else{
     echo json_encode(ison_decode($_POST, TRUE));
 }

6 Comments

I made mistake try now w/o json_encode because post data is json and we don't need json_encode.
the above code works. But i realized that only else statement is executing. !!! don't know why
beacouse you don't have or empty $_POST['json']... once again - check your post data structure. I made changes to my answer check and try again.
no way, if statement is not working. and echo $_POST does nothing. so output is same (JSON Error). The problem is empty $_POST['json'] . Tried your new code as well. Given some data to else like $dataElse = [ 'a', 'b', 'c' ]; echo json_encode($dataElse); it worked
I made changes try now.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.