0

I need to await a nodejs function that runs a node-fetch requisition but it's presenting "Internal Server Error". This is a lambda function on aws and the url target is public and accessible. What I'm doing wrong?

My code:

const AWS = require('aws-sdk');
const fs = require('fs');
const { v4: uuidv4 } = require('uuid');
const fetch = require('node-fetch');

async function fetchLogin(){
  const url = 'https://www.url.../test.txt';
  const res = await fetch(url);
  return res;
}

exports.index = async (event, context, callback) => {
  const fetchResult = await fetchLogin();
  return response(200,'text/html', fetchResult);
};

Sandro Benevides
  • 581
  • 2
  • 14
  • 2
    Isn't it the url you are fetching that is returning an Internal server error ? – HartWoom Jul 19 '21 at 12:24
  • @HartWoom no, because I can reach it with a browser – Sandro Benevides Jul 19 '21 at 12:28
  • 2
    That doesn't mean that you can do it via code...the browser will send a bunch of things like cookies and other headers. We have no idea what your `fetch` sends. If it's really exactly like this, then you don't send any headers. Perhaps you need some. – VLAZ Jul 19 '21 at 12:29
  • 1
    Do we have fetch api in nodejs? I think it's only available in browsers. – h-sifat Jul 19 '21 at 12:32
  • @h-sifat `const fetch = require('node-fetch');` at the top – VLAZ Jul 19 '21 at 12:34
  • 1
    @SandroBenevides try to log the result before returning the response. Also you can add a .catch() on fetchLogin(), there might be more details if an error is thrown – HartWoom Jul 19 '21 at 12:37
  • @VLAZ oh I see! – h-sifat Jul 19 '21 at 12:40
  • @HartWoom I tried to put the fetch() inside a try/catch but it returned a message saying the endpoint take too long to respond – Sandro Benevides Jul 19 '21 at 17:38
  • There are lots of reasons for a request to timeout. But you need to debug by yourself why, either with a tool like Postman or directly with your browser, you can access what is being sent, which way, etc... – HartWoom Jul 19 '21 at 17:46

0 Answers0