0

I am attempting to do a GET request to an external API using node-fetch. When I run the code, I get the below error:

UnhandledPromiseRejectionWarning: TypeError: Bearer [api key]​ is not a legal HTTP header value

The Bearer key is in JWT format and it is located in an external config file for privacy.

Here is my code:

let api_config = require('./api_config.json');
const fetch = require("node-fetch");

fetch(api_config.API_Domain + api_config.SUBJECTCODES_OPTIONS_URI, {
    method: 'GET',
    headers: { 
        'Authorization': "Bearer " + api_config.API_Key,
        "Content-type": "application/json",
        "Accept": "application/json", 
    }
}).then(res => res.json())
.then(json => console.log(json))
  • 4
    If your `API_Key` value has a line ending character in it (CR or LF), it would cause this problem. Without knowing the value though (or an obfuscated version of it; for example, with all numbers replaced with 'A' and all letters replaced with 'B', to protect the actual value), it's going to be a guessing game. – Heretic Monkey Aug 28 '21 at 17:08
  • You may also be blocked from setting the Authorization header; see [How to send authorization header with axios](https://stackoverflow.com/q/44245588/215552) for more information on that. – Heretic Monkey Aug 28 '21 at 17:29

1 Answers1

0

I had sam problem and same error with nextjs , I replace url_list[url] with encodeURI(url_list[url]) as below

 return NextResponse.redirect(
            encodeURI(url_list[url]),
            301
        )

encoding url soved my problem

Ghazaleh Javaheri
  • 1,829
  • 19
  • 25