Search code examples
arraysjsonsession-variableszapier-cli

Modify Returned Response


I am trying to get a Session Key response to use in my future request inside a Zapier Session Authentication, however the response back from a successful authentication is "OK: hbsdakjdkaskjdfvbasdkjh". I need the hbsdakjdkaskjdfvbasdkjh extracted and then saved as the session key variable in the below zapier cli code

I am a little new to parsing JSON, but I think the response is actually raw... I tried a Regex but couldn't figure out the right statement and wondered if someone could help point me in the right direction.

The session URL params etc are working, and the Session Key responding after the OK: is actually correct and one I can use for the remainder of the session manually....

 const options = {
  url: 'https://theconsole.domain.com.au/api/auth.pl',
  method: 'POST',
  headers: {
    'content-type': 'application/x-www-form-urlencoded',
    'accept': 'application/json'
  },
  params: {
    'AccountNo': bundle.authData.AccountNo,
    'UserId': bundle.authData.UserId,
    'Password': bundle.authData.Password
  },
}

return z.request(options)
  .then((response) => {
    response.throwForStatus();
    const results = z.JSON.parse(response.content);

    // You can do any parsing you need for results here before returning them


    return {
      'sessionKey': results.sessionKey
    };
  });

Solution

  • Cool, so if your response is not json, you'll want to remove the z.JSON.parse line, since that'll throw an error.

    As for pulling the key out of the response, success will depend on knowing that the response will always have a certain format. In this case, it sounds like it's OK: somekey.

    If we can safely assume that there'll only ever be one space () and it's directly before the key, you can try something like:

    // the rest of the code
    respone.throwForStatus(); // important, assuming the server gives a non-2xx code for a bad auth
    const sessionKey = response.content.split(' ')[1];
    return {sessionKey}; // shorthand for {sessionKey: sessionKey}
    

    Assuming those assumptions hold true, that should work!