Я отправляю почтовый запрос на API-флягу Python.Я возвращаю 200, и тело ответа регистрируется в консоли правильно.Однако, когда я пытаюсь передать его в переменную response, чтобы сохранить переменную, которую она не присваивает.
Я попытался записать переменную response и посмотреть, что на самом деле передается в нее.Если я пытаюсь войти в консоль при вызове запроса, он показывает, что он успешно содержит строку.Но когда я регистрирую его на консоли за пределами запроса, он отображается как неопределенный?
Это мой 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);
}
Это мой обработчик сообщений на python
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 posts' из моего звонка на Python