last day (14 days later) » 

10:52 AM
0
A: Bad json array Python post request to PHP

ettananyYou need to use data instead of json argument and add headers argument as well: requests.post(php_url, data=json_data, headers={'Content-Type': 'application/json', 'Accept':'application/json'}) Take a look at this in requests documentation More complicated POST requests In your PHP code your ...

 
Thanks for your answer. I already tried to use data but I have the same problem.
 
Did you add headers and you still have the same issue?
 
Yes I have the same issue with the headers :/
 
did you test with small hashes in your list?
 
When I remove json.dumps and headers, data_hash is not empty but it contains just the last hash as a string... not an array. I have the same error with a simple array like ['test', 'test2', 'test3']
 
10:52 AM
Test with the above headers and let me know if your issue persists
Please take a look at my edited answer
 
Hum it's seems to work with getJsonRawBody
    if($this->request->isPost()){
        $rawBody = $this->request->getJsonRawBody(true);
        foreach ($rawBody['data_hash'] as $value) {
            error_log($value);
        }
    }
why the getPost doesn't work ?
I'm using it always and I have no problem
 
because you need to specify another content-type value
your php code supposes that it would receive data submitted by your form not a json data
 
I tried to remove the headers and It works too without headers
because now it work with just getJsonRawBody
 
You may need to use 'application/x-www-form-urlencoded' if you want to treat your data like if it is submitted from a form
Do not forget to upvote and accept my answer :)
good luck!
 
So now if I have array in my json I have to use getJsonRowBody else I can use getPost
Thanks for you help
 
11:03 AM
You're welcome
 

last day (14 days later) »