错误400 - 来自PHP请求的Flask request.get_data()的错误请求

I'm doing an internal API across my servers. I want to transfer some JSON strings from PHP to Python.

On Python, I have this code:

from flask import request
from flask import jsonify
...

@app.route('/database/<string:token>', methods=['POST', 'GET'])
def sync(token):
    if token not in VALID_TOKENS:
        ...

    json = request.get_json(force=True)
    ...

...
if __name__ == "__main__":
    app.run(host='0.0.0.0', debug=False, port=6000)

On PHP-side, I have:

$payload = $datafromdatabase; 
$payload = json_encode($payload); 
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);  
curl_setopt($ch, CURLOPT_URL, $endpoint); 
curl_setopt($ch, CURLOPT_POST, true); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload); 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); 
curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
    'Content-Type: application/json',                                                                                
    'Content-Length: ' . strlen($payload))                                                                       
);
$result = curl_exec($ch); 

But Flask returns me, at every request:

127.0.0.1 - - [19/Feb/2017 20:08:00] "POST /database/token HTTP/1.1" 400 -

P.S: On Python, request.data and request.args are both None.

P.P.S.: Using requests.post(url, json=data) in Python works.

douxie0824
douxie0824 我编辑了,但问题不在于它。
3 年多之前 回复
dtlab08822
dtlab08822 $data_string在哪里定义?你正在计算来自那个未知变量的内容长度,而不是来自$payload
3 年多之前 回复
dongu189744321158
dongu189744321158 在调试模式下运行flask:app.run(host='0.0.0.0',debug=True,port=6000)。怎么了?
3 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐