如何将我的变量分配给我的API的字符串响应?
我送POST请求给一个Python API瓶。我在控制台中正确地返回200和身体的响应日志。然而,当我试图把它传递给了“回应”变量来存储它不会给变量。
我试图登录响应变量,看看什么是真正传递给它。如果我试图把它登录在请求调用控制台就说明它成功有一个字符串。但是当我登录到控制台的要求之外它显示为未定义?
这是我的Node.js做呼叫
function handleMessage(sender_psid, received_message) {
let response;
request({
"uri": "http://localhost:8090/givenMessage",
"method": "POST",
"json": received_message
}, (err, res, body) => {
if (!err) {
console.log('message sent to python!')
console.log(typeof body)
var string = JSON.stringify(body);
var objectValue = JSON.parse(string);
response = body; //parse the json body to string keep body in
} else {
console.error("Unable to send message to python:" + err);
}
});
// Check if the message contains text
/*if (received_message.text) {
// Create the payload for a basic text message
response = {
"text": `You sent the message: "${received_message.text}".`
}
} */
// Sends the response message
callSendAPI(sender_psid, response);
}
这是我的蟒蛇后处理程序
from flask import Flask
from flask import request
app = Flask(__name__) #create the app server to recieve post
@app.route('/givenMessage', methods = ['POST'])
def postJsonHandler():
print (request.is_json)
content = request.get_json()
print (content)
return 'JSON posted'
app.run(host = '0.0.0.0', port = 8090)
现在,当我把它传递给callSendAPI方法响应变量就是不确定的。我想它有串组输出“JSON发布”从我的蟒蛇电话后
回答如下:对不起,我回答了。 POST请求是缓慢,响应变量没有得到指定的时间。它被异步调用。我仅仅指刚把if语句里面我的方法等待后响应