Python Forum
Post JSON from python to PHP don't give expected result
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Post JSON from python to PHP don't give expected result
#1
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 !
Reply
#2
This seems like a PHP question rather than Python, the Python looks fine.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Looping through each images in a give folder Python druva 1 2,426 Jan-01-2025, 08:46 AM
Last Post: Pedroski55
  Python trivial endgame engine is not working as expected max22 0 1,571 Feb-24-2024, 04:41 PM
Last Post: max22
  math formula does not give the same result as bash script [SOLVED] AlphaInc 3 2,320 Apr-02-2023, 07:21 PM
Last Post: AlphaInc
  Python Split json into separate json based on node value CzarR 1 11,549 Jul-08-2022, 07:55 PM
Last Post: Larz60+
  difficulties to chage json data structure using json module in python Sibdar 1 3,140 Apr-03-2020, 06:47 PM
Last Post: micseydel
  applying and(&) ,or(|) in conditions does not result output correctly as expected Smiling29 4 4,137 Oct-21-2019, 01:39 AM
Last Post: ichabod801
  Palindrome in Python - result always false RavCOder 13 11,494 Oct-16-2019, 01:38 PM
Last Post: perfringo
  Post JSON dat and Image vijsi 2 3,396 Oct-03-2019, 01:35 PM
Last Post: vijsi
  Help with passing command line input to HTTP POST json sf05wrx 2 3,773 Feb-25-2019, 11:19 PM
Last Post: sf05wrx
  My code is taking longer time to give result rajeshwin 4 5,123 Feb-20-2019, 08:18 PM
Last Post: ichabod801

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.