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
  math formula does not give the same result as bash script [SOLVED] AlphaInc 3 921 Apr-02-2023, 07:21 PM
Last Post: AlphaInc
  Python Split json into separate json based on node value CzarR 1 5,477 Jul-08-2022, 07:55 PM
Last Post: Larz60+
  difficulties to chage json data structure using json module in python Sibdar 1 2,044 Apr-03-2020, 06:47 PM
Last Post: micseydel
  applying and(&) ,or(|) in conditions does not result output correctly as expected Smiling29 4 2,621 Oct-21-2019, 01:39 AM
Last Post: ichabod801
  Palindrome in Python - result always false RavCOder 13 6,602 Oct-16-2019, 01:38 PM
Last Post: perfringo
  Post JSON dat and Image vijsi 2 2,114 Oct-03-2019, 01:35 PM
Last Post: vijsi
  Help with passing command line input to HTTP POST json sf05wrx 2 2,381 Feb-25-2019, 11:19 PM
Last Post: sf05wrx
  My code is taking longer time to give result rajeshwin 4 3,258 Feb-20-2019, 08:18 PM
Last Post: ichabod801
  python hmac gave different result than php hash_hmac nadavvin 2 3,114 Feb-18-2019, 03:35 PM
Last Post: nadavvin
  Unexpected expected type error result MartinMaker 1 2,017 Feb-16-2019, 05:02 PM
Last Post: micseydel

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020