John Davidson

POST request payload in python not reaching PHP server

0 comments
Message:


I am trying to send a JSON payload with session request in python to a PHP server.
The session itself is established and cookies are accepted by the server but when I print out the $_POST, it returns an empty array.
It looks like the payload is not being sent with the request or maybe PHP doesn't recognize it.


Client Side - Python:


import json
import requests

url = 'https://prov.is.st.com/eventpost.php'
payload = {
'script': 'pyt.sh',
'status': 'Success'
}

s = requests.Session()
s.cookies.set('provisioning', cookie, domain='prov.is.st.com')
headers = {
'Content-Type': 'application/json'
}
s.headers.update(headers)
response = s.post(url, data=payload, verify=False)
print(response.text)

Server Side - PHP:


<?php
print_r($_POST);
if(isset($_POST['status'])){
$status = $_POST['status'];
}
else{
$status=0;
}

if (isset($_POST['script'])) {
$script=$_POST['script'];
} else {
$script="unknown";
}

if (isset($_COOKIE['provisioning'])) {
$cookie=$_COOKIE['provisioning'];
print "OK: Message accepted: [$status][$script]\n";
}
else {
print "ERROR: NO cookie provided.\n";
}

Output


Array
(
)
OK: Message accepted: [0][unknown]


My Answer:
The issue here is that you are sending the payload as form data in the Python code, but you are setting the Content-Type header to 'application/json'. This mismatch is likely causing the PHP server to not recognize the payload correctly.

To fix this issue, you can either send the payload as JSON data in the Python code or update the PHP server to accept form data. Here's how you can send the payload as JSON data in Python:

python
import json
import requests

url = 'https://prov.is.st.com/eventpost.php'
payload = {
'script': 'pyt.sh',
'status': 'Success'
}

s = requests.Session()
s.cookies.set('provisioning', cookie, domain='prov.is.st.com')
headers = {
'Content-Type': 'application/json'
}
s.headers.update(headers)
response = s.post(url, data=json.dumps(payload), verify=False)
print(response.text)


With this change, the payload will be sent as JSON data and the PHP server should be able to recognize and process it correctly.

Rate this post

4 of 5 based on 5434 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us