dthdlv9777 2017-02-19 20:25
浏览 224

错误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.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥20 关于#硬件工程#的问题,请各位专家解答!
    • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
    • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
    • ¥30 截图中的mathematics程序转换成matlab
    • ¥15 动力学代码报错,维度不匹配
    • ¥15 Power query添加列问题
    • ¥50 Kubernetes&Fission&Eleasticsearch
    • ¥15 報錯:Person is not mapped,如何解決?
    • ¥15 c++头文件不能识别CDialog
    • ¥15 Excel发现不可读取的内容