1

I'm trying to send a post request with Python to PHP (I'm using Phalcon PHP).

Python Code:

array_hash =["0x348eb29f3295fedc10b5b869c751fb5479585c7e65169512c3a5ff474bc0e85a", "0x63b2590119f7ea533ed93e6b2e6112410fbf49f99157bc6d7e0ce7469d3d23a8", "0xfbcdc555a3783b5cfa495ad7a5d14a159657a3f0f6b6a68583fb06ebdf984d70"]

json_data = json.dumps({"data_hash": array_hash})

requests.post(php_url, json=json_data)

My Python print array is good, I have this :

{  
   "data_hash":[  
      "0x348eb29f3295fedc10b5b869c751fb5479585c7e65169512c3a5ff474bc0e85a",
      "0x63b2590119f7ea533ed93e6b2e6112410fbf49f99157bc6d7e0ce7469d3d23a8",
      "0xfbcdc555a3783b5cfa495ad7a5d14a159657a3f0f6b6a68583fb06ebdf984d70"
   ]
}

But now when I'm trying to get it with PHP my array is empty and when I make a loop on the array I have this error :

Invalid argument supplied for foreach() 

Here is my PHP code :

if($this->request->isPost()){
    error_log($this->request->getPost('data_hash')[0]);
    foreach ($this->request->getPost('data_hash') as $value) {
        error_log($value);
    }
}

But when I send just json string like this :

{"test1": "hello", "test2": "world"}

I can get it in PHP and it works. So... What is wrong with my Python array ?

1 Answer 1

1

You may need to use data instead of json argument with 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 you need to use the following: (as your request specifies json for the content type)

$rawBody = $this->request->getJsonRawBody(true);
Sign up to request clarification or add additional context in comments.

8 Comments

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']
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.