2017-09-02 82 views
0

我已经创建了简单的azure函数(Http + C#),它返回简单的test.I想要返回azure函数对azure移动服务的响应。在我从移动服务调用存储过程之前,现在我试图调用那个azure函数(节点Js)想要从azure函数获取响应。 Azure的移动服务代码及以下如何从Azure移动应用程序服务调用HTTP(Azure函数)?


 

 
module.exports = { 
 
    "post": function (req, res, next) {  
 
      console.log("Started Application Running"); 
 
     var http = require("http");  
 
     var options = { 
 
      host: "<appname>.azurewebsites.net",   
 
      path: "api/<functionname>?code=<APIkey>", 
 
      method: "POST", 
 
      headers : { 
 
       "Content-Type":"application/json", 
 
       "Content-Length": { name : "Testing application"} 
 
      }  
 
     }; 
 
     http.request(options, function(response) { 
 
      var str = ""; 
 
      response.on("data", function (chunk) { 
 
      str += chunk; 
 
      res.json(response); 
 
      console.log("Something Happens"); 
 
      }); 
 
      response.on("end", function() { 
 
      console.log(str);    
 
      res.json(response); 
 
     });   
 
     }); 
 
     console.log("*** Sending name and address in body ***");   
 
    } 
 
};

这里是我蔚蓝的功能

using System.Net; 
 

 
public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, TraceWriter log) 
 
{ 
 
    log.Info("C# HTTP trigger function processed a request."); 
 

 
    // parse query parameter 
 
    //string name = req.GetQueryNameValuePairs() 
 
     // .FirstOrDefault(q => string.Compare(q.Key, "name", true) == 0) 
 
     //.Value; 
 
    string name = "divya"; 
 
    // Get request body 
 
    dynamic data = await req.Content.ReadAsAsync<object>(); 
 

 
    // Set name to query string or body data 
 
    name = name ?? data?.name; 
 

 
    return name == null 
 
     ? req.CreateResponse(HttpStatusCode.BadRequest, "Please pass a name on the query string or in the request body") 
 
     : req.CreateResponse(HttpStatusCode.OK, "Hello Welcome "); 
 
}

。可任何一个可以帮助我,我简单的蔚蓝功能的脚本?

+0

什么是你目前有确切的问题? – Mikhail

+0

当我运行移动服务时出现内部服务器错误。我试图解决我不知道确切的解决方案。 – divya

回答

0

您应使用req.write(data)发送请求主体,而不是Content-Length。有关如何执行此操作的示例,请参阅this answer

您的代码应该如下:

module.exports = { 
    "post": function (req, res, next) {  

    var http = require("http"); 

    var post_data = JSON.stringify({ "name" : "Testing application"}); 

    var options = { 
     host: "<appname>.azurewebsites.net",   
     path: "/api/<functionname>?code=<APIkey>", // don't forget to add '/' before path string 
     method: "POST", 
     headers : { 
     "Content-Type":"application/json", 
     "Content-Length": Buffer.byteLength(post_data) 
     }  
    }; 

    var requset = http.request(options, function(response) { 
     var str = ""; 
     response.on("data", function (chunk) { 
     str += chunk; 
     }); 

     response.on("end", function() { 
     res.json(str);   
     });   
    }); 

    requset.write(post_data); 
    requset.end(); 
    requset.on('error', function(e) { 
     console.error(e); 
    }); 

    } 
}; 
+0

我已经尝试过,但没有从我的移动服务API获取价值。请注意意外的连接失败错误。 – divya

+0

嗨,我用我的工作代码更新了我的答案。 –

相关问题