如何使用Azure移动服务API功能

时间:2013-06-12 23:09:29

标签: javascript ios mobile azure azure-mobile-services

WAMS中添加了 API 功能,我可以在其中定义自定义脚本。这似乎不赞成以前创建脚本表的做法。但是,我找不到任何关于如何使用它的描述。

哪些客户可以访问此功能?它可以在iOS或Javascript中使用吗?

enter image description here

3 个答案:

答案 0 :(得分:6)

关于此主题的更多帖子:http://blogs.msdn.com/b/carlosfigueira/archive/2013/06/14/custom-apis-in-azure-mobile-services.aspx(服务器端)和http://blogs.msdn.com/b/carlosfigueira/archive/2013/06/19/custom-api-in-azure-mobile-services-client-sdks.aspx(客户端)。

此外,由于您使用ios标记了您的问题,因此以下是您使用MSClient类实例调用API的代码:

如果您的API仅处理(接收/返回)JSON数据:

MSClient *client = [MSClient clientWithApplicationURLString:@"https://your-service.azure-mobile.net"
                                             applicationKey:@"your-application-key"];
[client invokeApi:@"calculator/add"
             body:nil
       HTTPMethod:@"GET"
       parameters:@{@"x":@7, @"y":@8}  // sent as query-string parameters
          headers:nil
       completion:^(id result, NSURLResponse *response, NSError *error) {
    NSLog(@"Result: %@", result);
}];

或者使用请求正文(POST):

[client invokeApi:@"calculator/sub"
             body:@{@"x":@7, @"y":@8}   // serialized as JSON in the request body
       HTTPMethod:@"POST"
       parameters:nil
          headers:nil
       completion:^(id result, NSHTTPURLResponse *response, NSError *error) {
    NSLog(@"Result: %@", result);
}];

如果您的API处理非JSON数据,您可以使用另一个/ /返回NSData对象的选择器:

NSData *image = [self loadImageFromSomePlace];
[client invokeApi:@"processImage"
             data:image
       HTTPMethod:@"POST"
       parameters:nil
          headers:nil
       completion:^(NSData *result, NSHTTPURLResponse *response, NSError *error) {
    NSLog(@"Result: %@", result);
}];

答案 1 :(得分:5)

答案 2 :(得分:5)

我也找到了这个帮助:

http://www.windowsazure.com/en-us/develop/mobile/tutorials/create-pull-notifications-dotnet

基本上您可以使用此终点格式访问自定义API:

https://service_name.azure-mobile.net/api/api_name

把它放在你的剧本上:

exports.get = function(request, response) {    
    response.send(200, "Hello World");
};

并在GET上设置您的API权限以允许 所有人 ,然后您可以使用浏览器或fiddler通过访问终点来测试您的API:

https://service_name.azure-mobile.net/api/api_name

如果您未更改权限,则必须在请求中输入以下标题代码:

GET https://service_name.azure-mobile.net/api/test HTTP/1.1
User-Agent: Fiddler
Content-type: application/json
X-ZUMO-APPLICATION: your-manage-key-here
相关问题