0

I have created simple azure Function (Http+C#) which returns simple test.I want to return response from azure functions to azure mobile services.Before i have invoked stored procedure from my mobile services.now i am trying to invoke that azure function(node Js) want to get back the response from azure functions. Azure mobile Service Code and my simple azure function script below

   

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

Here is my azure function

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 any one help me?

divya
  • 193
  • 3
  • 15

1 Answers1

1

You should send your request body by using req.write(data), not in Content-Length. See this answer for an example of how to do this.

And your code should look like below:

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

  }
}; 
Aaron Chen
  • 9,835
  • 1
  • 16
  • 28
  • i have tried but not getting value from my mobile service API.getting unexpected connection failure error. – divya Sep 04 '17 at 11:20