Get data from response headers

Options
Hi all! I try to upload video on youtube and use youtube data API

I send POST request to API and receive the response, but the data which i need is in response headers

How i can get access to response headers? Try $http_headers, but it isn't return the right data

Comments

  • Sean Montgomery
    Sean Montgomery Administrator

    ADMIN

    Options
    Hi ,

    The return from the API request is an object with all relavent information.

    var ret = API_Request

    the response headers would be inside ret.response.headers - see screenshot - https://mrkr.io/s/62865535d368286d46f30e9a/0

    the environment variable $http_headers are the request headers sent to Xano, not the API request being run inside your function stack. 
  • Evgeny Sporykhin
    Options
    yeah, but how i can get, for example, content-length in header? 
  • Ray Deck
    Ray Deck Trusted Xano Expert ✭✭✭
    Options
     I think to accomplish your goal you want to apply the "get" filter  to ret.headers, and pass the path of "content-length"


    Correction: see 's correct and detailed description of working with the array rather than object mapping below. 
  • Sean Montgomery
    Sean Montgomery Administrator

    ADMIN

    Options
    close - ret.response.headers is an array of key/value pairs in string form

    $http_headers is an object where the key of each object is the header key.

    You need to do a bit more work for ret.response.headers

    You can use Data Manipulation - Arrays - Array.findFirstElement

    Pass in the value content-length:

    You can assign that to a variable called header.

    Then you have the entire header in a variable. To extract the number from that, you would want to do the following

    var:length = var:header|split(":")|last|trim
  • Evgeny Sporykhin
    Options
     thanks! i will try that
  • Evgeny Sporykhin
    Options
     thanks! i tried your first solution and it didnt work)
  • Ambroise
    Ambroise Member
    Options

    Here is how I managed to access headers, through a Lambda.

    const headers = $env.$http_headers
    console.log($env, headers);
    return headers;
    

  • Ambroise
    Ambroise Member
    Options

    And then, how do you test this? Because reading the Authentication header is one thing, prefilling it for in-editor testing is another…