Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

117
Visualizações
How do I get data as JSON format from the IBM Cloud with a HTTP Request using JavaScript?

When I click on "GET DATA" in my App, I would like to access the data in my IBM Cloud with an HTTP request. I need the data in JSON format. This should be implemented with JavaScript. My current code is here:

function httpRequest() {
  const xhr = new XMLHttpRequest()
//open a get request with the remote server URL
xhr.open("GET", "https://<orgID>.internetofthings.ibmcloud.com/api/v0002/device/types/<typeID>/devices/<deviceID>/state/<logicalInterfaceID>" )
//send the Http request
xhr.send()

//EVENT HANDLERS

//triggered when the response is completed
xhr.onload = function() {
  if (xhr.status === 200) {
    //parse JSON datax`x
    data = JSON.parse(xhr.responseText)
    console.log(data.count)
    console.log(data.products)
  } else if (xhr.status === 404) {
    console.log("No records found")
  }
}

//triggered when a network-level error occurs with the request
xhr.onerror = function() {
  console.log("Network error occurred")
}

//triggered periodically as the client receives data
//used to monitor the progress of the request
xhr.onprogress = function(e) {
  if (e.lengthComputable) {
    console.log(`${e.loaded} B of ${e.total} B loaded!`)
  } else {
    console.log(`${e.loaded} B loaded!`)
  }
}
}
.btn {
  cursor: pointer;
  background-color: #555;
  color: #fff;
  display: inline-block;
  padding: 5px;
  margin-left: auto;
  margin-right: auto;
}
<!DOCTYPE html>
<html lang="de">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="src/css/styles.css"/>
    <script src="src/js/script.js"></script>
    <title>GET DATA</title>
    <div class="btn" onclick="httpRequest()">
      GET DATA
    </div>   
  </head>
  <body>
  </body>
</html>

The placeholder orgID, typeID, deviceID, logicalInterfaceID in my code etc. have of course been replaced by the correct ID.

The problem is, I don't know how to include the username and password in the URL so that I can access the IBM Cloud.

https://www.ibm.com/docs/en/mapms/1_cloud?topic=reference-application-rest-apis

about 4 years ago · Santiago Gelvez
1 Respostas
Responde à pergunta

0

Checkout this post on What is -u flag on cURL actually doing?

You can base64 encode your credentials using the native btoa() function then set them in the authorization req header.

var xhr = new XMLHttpRequest();
xhr.open( "GET", "https://<orgID>...");
xhr.setRequestHeader("Authorization", `Basic ${btoa('username:password')}`);    
xhr.send();
about 4 years ago · Santiago Gelvez Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda