0

I have a test environment of two Linux machines - one with with a PHP script, the other with a python script. I am trying to send an array of dictionaries as a JSON to the PHP script using the python. At this point, the PHP script does nothing but print what it received:

// Contents of set-data.php
<?php print_r ($_POST); ?>

The python script is as follows:

# Contents of test.py
import requests
import json

headers = {
  'Content-Type': 'application/json'
}
url = "http://mylocation/api/set-data.php"
payload = {'data': '[{"1": "Test_1", "2": "test_2"}]'}
   
response = requests.request("POST", url, headers=headers, data=payload)
print(response.text)

No matter how I run this in my test environment, the response is empty

// Result from set-data.php
Array
(
)

I have tried to change the data field in the request to json, tried to set the payload to a json.dumps object, and various other techniques, but nothing seems to work. However, if I send this information as a urlencoded form from the python script, it works.

headers = {
  'Content-Type': 'application/x-www-form-urlencoded'
}
payload='data=%5B%7B%221%22%3A%20%22Test_1%22%2C%20%222%22%3A%20%22test_2%22%7D%5D'

I know that the machine running the PHP script is accepting the JSON because when I do this from my location machine using Postman the result is correct.

// Result from set-data.php sending from local machine
Array
(
    [data] => [{"1": "Test_1", "2": "test_2"}]
)

Does anyone know why? I think something may be misconfigured on the python machine, but what?

Apolymoxic
  • 730
  • 14
  • 34

0 Answers0