Decoding the response body from azure functions

2019-09-16 02:09发布

问题:

I am trying out azure functions. I understood about the working of azure functions and necessary bindings for combining the functions with other azure services.

Scenario:

Posting JSON through request's body to azure functions. Store the JSON in documentDB and return the same JSON as response.

It works properly when invoking the api in azure console. And it also works in postman. But weirdly, It does not work in my react app.

Problem:

In the above image, as you can see, the response is returned by azure functions in react app. But the body is a ReadableStream object. There is also a locked property. I have tried in both function and anonymous authorisation level. Both gave the same results. I am puzzled about this problem.

My thoughts:

Should I have an object to read the stream object or is it the problem of authorisation. It works perfectly well in postman and in azure console but not in react app. What am I missing ?

Register(POST) Azure Function

This function gets some request body parameters and store it in documentDB using output bindings. Here is the code:

module.exports = function(context, req) {
    context.log('Node.js HTTP trigger function processed a request. RequestUri=%s', req.originalUrl);

    if (req.query.name || (req.body)) {
        let eventId = req.body.eventId;
        let name = req.body.name;
        let purpose = req.body.purpose;
        let dateArray = req.body.dateArray;
        let location = req.body.location;

        let eventObj = {
            "id": eventId,
            "name" : name,
            "purpose" : purpose,
            "dateArray": dateArray,
            "location": location,
            "attendees": []
        }

        context.bindings.outputDocument = eventObj;
        context.res = {
            // status: 200, /* Defaults to 200 */
            body: eventObj
        };
    }
    else {
        context.res = {
            status: 400,
            body: "Please pass a name on the query string or in the request body"
        };
    }
    context.done();
};

React (post to azure function)

This is an react-redux app. Below is the code for POSTing to azure function with some request body parameters. The output of this code. i.e the response is already shown in the screenshot where response body cannot be decoded. But for the same http call, both postman and azure console works fine.

export function registerEvent(eventId, name, purpose, dateArray, location, attendees) {
  return dispatch => {
    return fetch('https://letsmeetup-test1.azurewebsites.net/api/HttpTriggerNodeJS2', {credentials: 'omit',
      method: 'POST',
      headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/json'
      },
      body: JSON.stringify({'eventId': eventId, 'name' : name, 'purpose' : purpose, 'dateArray': dateArray, 'location': location, 'attendees': attendees})})
      .then(res => {
        if (res.status !== 200) {
          let status = res.status;
          console.log('error in posting event');
        }
        console.log("printing response from azure functions");
        console.log(res);
        return res.json();
      })
      .then(json => storeEventId(json))
  };
}