Search code examples
phpjsonfile-get-contents

file_get_content('php://input') return wrong data


i want to get full json data sent to my page, im using file_get_contents('php://input') and i convert it into json using json_decode(). But for discord embeds, file_get_contents() return wrong data.

Json string : {"embeds":[{"title":"Hello!","description":"Hi!"}]}

file_get_contents() return : embeds=title&embeds=description

i use python requests module to send json :

import requests

data = {"embeds":[{"title":"Hello!","description":"Hi! :grinning:"}]}

headers = {'Content-Type': 'application/json'}

r = requests.post('https://example.com', data=data, headers=headers)

print(r.text)

alternative to file_get_contents() or something else ?


Solution

  • The problem is from the request in the Python script. data parameter should send as a string for JSON mode. add import json to your code and use data=json.dumps(data) instead of data=data.

    More information about json.dumps is here: https://docs.python.org/3/library/json.html

    r = requests.post('https://example.com', data=json.dumps(data), headers=headers)