1

I am trying to call fetch with credentials to hit a https api

url: https://{apikey}:{password}@{hostname}/admin/api/{version}/{resource}.json

I try this:

  var apikey = "mykey";
  var password = "mypass";
  var hostname = "myhost";
  var version = "version";
  var resource = "resource";
  var API_URL = `https://${apikey}:${password}@${hostname}/admin/api/${version}/${resource}.json`;
  fetch(API_URL, {
    credentials: "omit"
  })
    .then((response) => response.json())
    .then((data) => console.log(data));

but return this error: Failed to execute 'fetch' on 'Window': Request cannot be constructed from a URL that includes credentials

  • Are you sure that the the proper URL format for the API? – imvain2 Oct 01 '20 at 18:49
  • I omitted to write the original values of the strings, because I cannot share that information.. – franco moretti Oct 01 '20 at 18:51
  • 2
    I meant the URL structure isn't valid. Typically APIs are expecting the login credentials sent via headers, query string or POST paramaters. – imvain2 Oct 01 '20 at 18:53
  • Similar questions with similar responses about structure: https://stackoverflow.com/questions/42518520/request-cannot-be-constructed-from-a-url-that-includes-credentials or https://stackoverflow.com/questions/45067331/request-with-url-that-includes-credentials – Marc Oct 01 '20 at 19:20

1 Answers1

0

Just send credential with Header Authorization Bearer or Basic with base64 encoded credentials

svyat1s
  • 868
  • 9
  • 12
  • 21