如何读取 Azure App Service 中的请求正文?
How to read the body of the request in Azure App Service?
我正在尝试在 Azure 应用服务(从 Azure 移动服务迁移)中创建一个 EasyAPI。在 swift 中使用以下命令从 iOS 应用程序发送消息:
let query: Dictionary<String, AnyObject> = ["name": theName]
let param: Dictionary<String, AnyObject> = ["collectionName": theCollectionName, "query": query]
AOAppDelegate.client!.invokeAPI("FMDataAPI", body: param, HTTPMethod: "POST", parameters: nil, headers: nil, completion: {(objects, httpResponse, error) in
if error == nil {
//Process response
} else {
print(error!.userInfo)
}
})
在 API 我在 EasyAPI MyEasyAPI 中有以下 Javascript 代码:
module.exports = {
"post": function (req, res, next) {
console.log("---------------------------------------")
console.log(req.body)
},
但 body 仍未定义。
有什么建议吗?
谢谢,
GA
您需要在调用 easy API 之前调整应用程序。当您添加中间件时,为时已晚。幸运的是,bodyparser 已经为您实现。请注意,通常情况下,您需要做一些需要 body 的事情 - 比如 POST - 才能做到这一点。
由于这是迁移的移动服务,您需要按照移动服务的说明进行操作 - 为应用服务记录的内容通常仅适用于升级后的网站(即应用服务上的网站 运行尚未迁移)。
好消息是,如果您需要,我们可以为您提供一些帮助。查看节点模块:https://github.com/Azure/azure-mobile-apps-node-compatibility 了解更多信息。
感谢您的建议。
我在 Azure Mobile Apps iOS 客户端的 github 项目中报告了一个问题,他们建议问题可能出在节点包 azure-mobile-apps 的版本中,事实就是如此。
azure-mobile-apps 包的版本是 2.0.0。我更新到2.1.0后req.body开始接收数据
这里有 link 到 Gihub issue discussion。
再次感谢您的建议。
GA
我的自定义 API 有一个类似的用例,并通过查询语句获取参数数据 - 我使用了 GET 调用,但这在这里应该没有任何区别:
module.exports = {
get: function (req, res, next) {
var param = req.query.completed;
console.log(param);
}
};
iOS 端带有附加参数字典的调用如下所示:
[self.client invokeAPI:@"resetMyItems"
body:nil
HTTPMethod:@"GET"
parameters:@{@"completed": @(self.completed)}
headers:nil
completion:^(id result, NSHTTPURLResponse * _Nullable response, NSError * _Nullable error) {
if (error == nil) {
NSLog(@"Got answer from my own API: %@", result);
}
else {
NSLog(@"Something went wrong with POST api call: %@", error);
}
}
];
我搜索了很长时间才发现,您在 iOS 端的 API 调用中附加的参数与您请求的查询语句中的参数完全相同。
希望对您有所帮助:)
我正在尝试在 Azure 应用服务(从 Azure 移动服务迁移)中创建一个 EasyAPI。在 swift 中使用以下命令从 iOS 应用程序发送消息:
let query: Dictionary<String, AnyObject> = ["name": theName]
let param: Dictionary<String, AnyObject> = ["collectionName": theCollectionName, "query": query]
AOAppDelegate.client!.invokeAPI("FMDataAPI", body: param, HTTPMethod: "POST", parameters: nil, headers: nil, completion: {(objects, httpResponse, error) in
if error == nil {
//Process response
} else {
print(error!.userInfo)
}
})
在 API 我在 EasyAPI MyEasyAPI 中有以下 Javascript 代码:
module.exports = {
"post": function (req, res, next) {
console.log("---------------------------------------")
console.log(req.body)
},
但 body 仍未定义。
有什么建议吗?
谢谢,
GA
您需要在调用 easy API 之前调整应用程序。当您添加中间件时,为时已晚。幸运的是,bodyparser 已经为您实现。请注意,通常情况下,您需要做一些需要 body 的事情 - 比如 POST - 才能做到这一点。
由于这是迁移的移动服务,您需要按照移动服务的说明进行操作 - 为应用服务记录的内容通常仅适用于升级后的网站(即应用服务上的网站 运行尚未迁移)。
好消息是,如果您需要,我们可以为您提供一些帮助。查看节点模块:https://github.com/Azure/azure-mobile-apps-node-compatibility 了解更多信息。
感谢您的建议。 我在 Azure Mobile Apps iOS 客户端的 github 项目中报告了一个问题,他们建议问题可能出在节点包 azure-mobile-apps 的版本中,事实就是如此。
azure-mobile-apps 包的版本是 2.0.0。我更新到2.1.0后req.body开始接收数据
这里有 link 到 Gihub issue discussion。
再次感谢您的建议。
GA
我的自定义 API 有一个类似的用例,并通过查询语句获取参数数据 - 我使用了 GET 调用,但这在这里应该没有任何区别:
module.exports = {
get: function (req, res, next) {
var param = req.query.completed;
console.log(param);
}
};
iOS 端带有附加参数字典的调用如下所示:
[self.client invokeAPI:@"resetMyItems"
body:nil
HTTPMethod:@"GET"
parameters:@{@"completed": @(self.completed)}
headers:nil
completion:^(id result, NSHTTPURLResponse * _Nullable response, NSError * _Nullable error) {
if (error == nil) {
NSLog(@"Got answer from my own API: %@", result);
}
else {
NSLog(@"Something went wrong with POST api call: %@", error);
}
}
];
我搜索了很长时间才发现,您在 iOS 端的 API 调用中附加的参数与您请求的查询语句中的参数完全相同。
希望对您有所帮助:)