2017-09-02 21 views
0

シンプルなテストを返すシンプルなアズール関数(Http + C#)を作成しました。アズール関数からの応答を空白のモバイルサービスに戻したいと思います。私のモバイルサービスからストアドプロシージャを呼び出す前に、関数(ノードJs)は、紺色の関数からの応答を取り戻そうとしています。 AzureのモバイルサービスコードとここAzure Mobile App ServicesからHTTP(Azure Functions)を呼び出す方法は?


 

 
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 "); 
 
}

ある.Canいずれか私を助けて?

+0

あなたが現在持っている正確な問題は何ですか? – Mikhail

+0

私はモバイルサービスを実行するときに内部サーバーのエラーを取得しています。私は私が正確な解決策を知らない解決しようとしました。 – divya

答えて

0

ではなく、req.write(data)を使用してリクエスト本体を送信する必要があります。これを行う方法の例は、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

こんにちは、私は私の作業コードで私の答えを更新しました。 –