Exported variables from body not working

Hi there,

I stumbled upon APId recently and have been testing it out. It’s been working well, but I’m currently stuck on a test where I’m trying to export a variable from a response body and use it in another test.

Here’s the transaction steps that I’m having a bit of trouble with:

  - id: "GET /favorites"
    steps:
      - id: "authentication"
        request:
          method: POST
          endpoint: "{{ var.api_url }}/tokens"
          headers:
            "Content-Type": "application/json"
          body: |
            {
              "email": "{{ env.EMAIL }}",
              "password": "{{ env.PASSWORD }}"
            }
        export:
          auth_token: "{{ response.body.token }}"
      - id: "Get all favorites for the account"
        request:
          method: GET
          endpoint: "{{ var.api_url }}/favorites"
          headers:
            "Authorization": "Bearer token={{ authentication.auth_token }}"
        expect:
          code: 200

The step to request the token is working fine - I verified the body in the test but didn’t include it here. The response to request the token is the following:

{
  "token": "<api token>"
}

When I run the tests, APId is saying that authentication.auth_token isn’t found:

GET /favorites:
    FAIL        Get all favorites for the account
        request: GET https://<api_url>/favorites
        errors:
            prepare:
                interpolating step header value: authentication.auth_token: key not found

I’ve read the docs and seen the examples, but the examples only use exporting header values into the variable. It’s not clear if I can use the response body to export variables as well. I also tried to export response.token with no success.

Am I missing something else in this test? Any help will be greatly appreciated. Thanks!

I figured out the problem - I realized I was exporting the variable incorrectly. It needed to be the JSON path without any type of interpolation:

export:
  auth_token: response.body.token

As always, posting about an issue surfaces the solution - rubber duck debugging at its finest :laughing: Hopefully this helps someone else who might have gotten stuck as well!

1 Like

We really need to find a way of getting notifications on these. :smile: docs updated, thanks for pointing it out