Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Would like to be able to pass JSON list retrieved from one API to another #791

Open
danielbprice opened this issue Aug 29, 2022 · 5 comments
Labels
enhancement New feature or request topic: template

Comments

@danielbprice
Copy link

I believe this is issue is adjacent to but different from #790 and #347.

I have an API from which I can retrieve a list of objects. I can extract from that API's response a list which I would like to submit to another API. What I would like to do looks roughly like this:

GET {{url}}/api/devices

HTTP/* 200
[Captures]
all_devices: jsonpath "$[*]['id']"

# -- Create CSV Report of device activity from list of devices
POST {{url}}/api/report
Accept: text/csv
Content-Type: application/json
{
    "devices": {{all_devices}}
}

What I am finding is that this is impossible because the collection value is not allowed to be rendered:

error: Unrenderable variable
  --> hurl-tests/scandata-csv.hurl:45:20
   |
45 |     "devices": {{all_devices}}
   |                  ^^^^^^^^^^^ value [ecd3f0a9-8b58-492b-8770-7cfdfa4fa2d0,f7f75e86-7326-4c63-b35e-e3a4669e6ae5,fec5917d-c73c-4cf8-873f-1e715e589da3] can not be rendered

I sort-of understand why this is the case, but on the other hand, the data came from JSON, and clearly can be rendered to JSON, so the limitation here seems slightly arbitrary. I guess the problem is that there is no syntax to tell it that the data should be rendered out as JSON?

In my ideal world, this collection would render to JSON, and I could use this value in the second API call. Thanks!

@fabricereix
Copy link
Collaborator

Yes, we should be able to sent JSON Object/List.
we could add an helper funtion within the template (similar to #312, #606)

{{all_devices | to_json}}

@jcamiel jcamiel added enhancement New feature or request topic: template labels Jan 10, 2024
@AdmiralRazorbeard
Copy link

AdmiralRazorbeard commented Nov 22, 2024

Pretty sad it's not available yet as it seems like a pretty common scenario when working with apis (still I'm very grateful to hurl devs 🙏) . I have the exact same need as daniel.

In my workflow I need to chain the whole json body/ parts of it from one request to another. Is there any workaround to achieve the same ? Maybe by storing the json body response in a temporary file and re-using it somehow ?

@jcamiel
Copy link
Collaborator

jcamiel commented Nov 22, 2024

Hi @AdmiralRazorbeard

If you need to capture / reuse the whole body of a response, this is rather simple, you can just capture the body of a response and reinject it. For instance:

GET https://foo.com
HTTP 200
[Captures]
the_body: body

POST https://bar.com
```
{{the_body}}
```
HTTP 200

I think it should work

@fiedlr
Copy link

fiedlr commented Jan 31, 2025

Hi @AdmiralRazorbeard

If you need to capture / reuse the whole body of a response, this is rather simple, you can just capture the body of a response and reinject it. For instance:

GET https://foo.com
HTTP 200
[Captures]
the_body: body

POST https://bar.com

{{the_body}}

HTTP 200

I think it should work

Thanks so much for your help, I confirm it works! But one needs to set Content-Type header to application/json.

@jcamiel
Copy link
Collaborator

jcamiel commented Jan 31, 2025

Yes or you can use a json hint also:

POST https://bar.com
```json
{{the_body}}
```
HTTP 200

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request topic: template
Projects
None yet
Development

No branches or pull requests

5 participants