0

I would like to interact with a website written in PHP using Python. It pulls its data from a form with method `POST`. Now I have tried to set the values in `$_POST` by doing:
url = 'https://www.example.com/myscript.php'

data = {"PcID":"AJFJ-01AR","email":"email","refc":"","startBtn":"Start"}

result = requests.post(url, json=data).content

But I am sadly unable to set the values PHP receives with this (I am returned the page with form instead of after, meaning not all values are set). The data variable is copy-pasted from what PHP receives using the form on the website (echo json_encode($_POST);). Am I generally wrong in what I am doing? It wouldn't be hard for me to just give the values with a regular GET request but I am still wondering why this would not work.
Thank you!

3
  • 4
    You want data=data, not json=data. Commented Mar 20, 2022 at 11:01
  • Thank you, I will try that! From this post I understood that it would be json=data. stackoverflow.com/questions/15900338/… Commented Mar 20, 2022 at 11:03
  • If you want to send JSON data. But PHP doesn't parse JSON data into $_POST, only percent-encoded requests (the default for HTML forms). Commented Mar 20, 2022 at 11:04

1 Answer 1

0

The comment by deceze fixed it (thank you!). It should be result = requests.post(url, data=data).content not result = requests.post(url, json=data).content.

Sign up to request clarification or add additional context in comments.

Comments