Quantcast
Channel: DHIS2 Community - Latest topics
Viewing all articles
Browse latest Browse all 1891

Fetch request to web API returns HTML instead of JSON

$
0
0

Hello everyone,

I’ve been trying to connect a script to the web API with a custom token, as shown below. However, each time, it returns an HTML file instead of a JSON response. The HTML seems to be the login page. I saw here that this sometimes happens when you’re not logged in. I’m sure I’m doing the request wrong, so could you help me figure it out?

const TOKEN = "my-token"

fetch("https://qa.dhis2-minsal.org/api/users", {
    headers: {
        "Authorization": "ApiToken " + TOKEN,
        "Accept": "application/json"
    }
})
.then(resp => {
    console.log(resp);
    return resp.json();
})
.then(data => {
    console.log(data);
});

I get this response:

undefined:1
<!DOCTYPE HTML>
^

SyntaxError: Unexpected token '<', "<!DOCTYPE "... is not valid JSON
    at JSON.parse (<anonymous>)
    at parseJSONFromBytes (node:internal/deps/undici/undici:5731:19)
    at successSteps (node:internal/deps/undici/undici:5712:27)
    at fullyReadBody (node:internal/deps/undici/undici:4609:9)
    at process.processTicksAndRejections (node:internal/process/task_queues:105:5)
    at async consumeBody (node:internal/deps/undici/undici:5721:7)

Node.js v22.13.0

Thanks in advance!

2 posts - 2 participants

Read full topic


Viewing all articles
Browse latest Browse all 1891

Trending Articles