1

I'm using an Azure function to do some work, all is good except that I can not get the response body from the result:

module.exports = async function (context, req) {
    context.log('JavaScript HTTP trigger function processed a request.');    
    const fetch = require('node-fetch');
    const myURL= (req.query.apiURL|| (req.body && req.body.apiURL));

    fetch(myURL)
        .then(data => {
            if (!data.ok) {
                throw new Error('some error occurred');
            }

            return data;
        })
        .then(data => data.text())
        .then(text =>
            context.res = {
                body: text //here is the problem
            });      
}

function.json

{
  "bindings": [
    {
      "authLevel": "anonymous",
      "type": "httpTrigger",
      "direction": "in",
      "name": "req",
      "methods": [
        "get",
        "post"
      ]
    },
    {
      "type": "http",
      "direction": "out",
      "name": "res"
    }
  ]
}

Fix

The empty response was related to using an async method without await so just remove the async or use await with async.

Tarek
  • 43
  • 1
  • 7
  • try this `fetch(someURL).then(res => res.text()).then(body => console.log(body));` – Rio A.P Jan 19 '21 at 13:50
  • @RapSherlock yes like that it works for sure, but it's an azure function and I want to return result on the request response when calling the function separately – Tarek Jan 19 '21 at 13:53
  • I want just to get the request response after calling the Azure function from outside. with the `console.log` it's just returning result in console :) – Tarek Jan 19 '21 at 14:02
  • please try my edit :) – Rio A.P Jan 19 '21 at 14:05
  • thanks for the edit, but it still the same (200 Ok with empty response), it's related to how the Azure function work – Tarek Jan 19 '21 at 14:08
  • i see, you mean the problem is not in the fetch right ? sorry about that, I have no experience with Azure. – Rio A.P Jan 19 '21 at 14:13

3 Answers3

1

Please do not use lambda expressions, you need to execute them like this, and there will be a result (not sure what you are doing, but I think there is something wrong with your design):

const fetch = require('node-fetch');
    
module.exports = async function (context, req) {
    context.log('JavaScript HTTP trigger function processed a request.');    
    const myURL= (req.query.apiURL|| (req.body && req.body.apiURL));

    fetch(myURL)
        .then(
            //some logic here.
            context.res = {
                body: "This is a test." //here is the problem
            }
        );      
}
Cindy Pau
  • 13,085
  • 1
  • 15
  • 27
  • 1
    Thanks! my issue was using an async method without `await`. The post is updated with the solution. – Tarek Jan 21 '21 at 09:28
1

Enhanced async/await, version of @BowmanZhu

    const fetch = require('node-fetch');
    module.exports = async function (context, req) {
      
     try{  
      context.log('JavaScript HTTP trigger function processed a request.');    
      const myURL= (req.query.apiURL || (req.body && req.body.apiURL)),
            fetchResp = await fetch(myURL),
            resBody =fetchResp.text();
      
      /** TODO LOGIC **/

      context.res = {
                        status:200,
                        body: resBody 
                     };
    
     }catch(err){
       context.res = {
                        status:500,
                        body: err.message
                     };
     }
    }
  • Thanks! it's well enhanced, my issue was using an async method without `await`. The post is updated with the solution – Tarek Jan 21 '21 at 09:28
0

In my case, I didn't updated the environment variable for database.