Python Forum
Post JSON from python to PHP don't give expected result - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Post JSON from python to PHP don't give expected result (/thread-20764.html)



Post JSON from python to PHP don't give expected result - pgagnebin - Aug-29-2019

Hello.
I need to send JSON data from python to PHP.
Python and PHP are located on different servers.
I test with this python code :
import requests import json url = ".../rcv_json.php" headers = {'Content-type': 'application/json', 'Accept': 'text/plain'} # data to be sent to api myjson={"firstName": "Jane","lastName": "Doe","hobbies": ["running", "sky diving", "singing"],"age": 35,"children": [{"firstName": "Alice","age": 6},{"firstName": "Bob","age": 8}]} # myjsond for tests myjsond = json.dumps(myjson, sort_keys=False, indent=1, separators=(',', ':'), skipkeys=True) print(myjson) print("---") print(myjsond) print("---") # sending post request and saving response as response object r = requests.post(url, myjson, headers) # extracting response text #r.text print(r.text)
JSON data has hierarchical data, as it's possible to check with print(myjson).

The php code (rcv_json.php) is simply next (it's php, not python Tongue ):
<?php var_dump($_POST); ?>
And the printed result is :
Output:
array(5) { ["firstName"]=> string(4) "Jane" ["lastName"]=> string(3) "Doe" ["hobbies"]=> string(7) "singing" ["age"]=> string(2) "35" ["children"]=> string(3) "age" }
The data on sub-hierarchy (the hobbies and children) are missing.

I don't find what I do wrong...
If I try to send myjsond data, $_POST array is empty in php.

Thank's for help !


RE: Post JSON from python to PHP don't give expected result - micseydel - Sep-04-2019

This seems like a PHP question rather than Python, the Python looks fine.


This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.