aaron lilly
aaron lilly

Reputation: 274

How do i put headers in an api call?

I am making an api call to

http(s)://{hostaddress:port}/com.broadsoft.xsi-actions/v2.0/user/{userid}/services/callcenter

I have changed the appropriate information in the URL to reflect the correct host address/port / user id.

When that is complete, the page requests that I log on with a Username and Password.

I can manually enter this information, and receive the XML that I need. This is not Ideal. I would rather have a form that passess in this information.

To my understanding, this information is passed within the "headers". I have tried to look up how to do this, and even attempted using postman without much luck. I am not sure how to do this.

function loadDoc() {
  var xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
      myFunction(this);
    }
  };
  xhttp.open("GET", "http(s)://{hostaddress:port}/com.broadsoft.xsi-actions/v2.0/user/{userid}/services/callcenter", true);
  xhttp.send();
}

per w3 schools I can use setRequestHeader() which adds a label/value pair to the header to be sent.

I have tried

function loadDoc() {
  var xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
      myFunction(this);
    }
  };
  xhttp.open("GET", "http(s)://{hostaddress:port}/com.broadsoft.xsi-actions/v2.0/user/{userid}/services/callcenter", true);
  xhttp.send();setRequestHeader(Username:myUserName,Password:myPassword);
}

with no resolution. once i get this working i will set up the form and pass in the values.

Upvotes: 2

Views: 232

Answers (2)

Dario
Dario

Reputation: 6280

You are using setRequestHeader in the wrong way and calling send before setting headers in any case.

Try with

xhttp.setRequestHeader('Username', myUserName); 
xhttp.setRequestHeader('Password', myPassword);
xhttp.send(); // only call send after setting up the headers

Upvotes: 3

SkillGG
SkillGG

Reputation: 686

xhttp.setRequestHeaders(name, value) and it should be invoked before xhttp.send(). mdn

Upvotes: 1

Related Questions