2017-09-02 84 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

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

相關問題