Network API HTTP/s block - Response header data?

The API I’m accessing returns the rate limit status in the response header. As I have to make concurrent calls obeying the rate limit is important.

I think the HTTP/s block only returns the response body. Is there a way to get the response header? If not do you have a suggestion on what to do about the rate limit issue?

Thanks,
Tim

Hi Tim,

It is possible with custom JS code injected into the logic. Here’s an example:
UI Builder - ConsoleDemo - Backendless 2022-06-16 14-17-30

The Custom Code block has the following:

var request = new XMLHttpRequest();

return new Promise(function(resolve, reject) {
  var headerMap = {};
  request.open("GET", "https://quotes.rest/qod?language=en", true);
      
  request.onreadystatechange = function() {
    if (request.readyState == this.DONE) {
      console.log( request.status);
        if (request.status >= 200 && request.status < 300) {
          resolve({responseText:request.responseText, headers:headerMap});
        } 
        else {
          reject("Error, status code = " + request.status)
        }
    }
    
    if(this.readyState == this.HEADERS_RECEIVED) {
  
      // Get the raw header string
      var headers = request.getAllResponseHeaders();
  
      // Convert the header string into an array
      // of individual headers
      var arr = headers.trim().split(/[\r\n]+/);
  
      // Create a map of header names to values
  
      arr.forEach(function (line) {
        var parts = line.split(': ');
        var header = parts.shift();
        var value = parts.join(': ');
        headerMap[header] = value;
      });
    }
  }
  request.send();
})

Make sure to select return result:

The sample code above runs a GET operation. You’d need to modify the code to use the URL of your external service with the required request headers and body.

Regards,
Mark

I should have clarified this is for an API Service running in cloud code. Will this still work?

Tim

Yes, this will work any https:// resource.

Thanks, @mark-piller!

An no reason why I can use this for a POST or PUT, right? I can look up some code examples for that I think.

Tim

You should be able to use that code for any HTTP operation as long as you modify the request parameters.

Regards,
Mark