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

Option to get captured data without using --json #3208

Open
Phantop opened this issue Aug 31, 2024 · 2 comments
Open

Option to get captured data without using --json #3208

Phantop opened this issue Aug 31, 2024 · 2 comments
Labels
enhancement New feature or request

Comments

@Phantop
Copy link

Phantop commented Aug 31, 2024

Problem to solve

Currenty, the best way to get a captured value out of hurl to use for other shell scripting seems to be something like:

hurl --json sw.hurl | jq '.entries[-1].captures[] | select(.name=="json").value[]' -r

In this case, I have a captured variable name json (which itself was using Xpath and JSONpath to get some obscured JSON data from a webpage) and it's returning an array which I could then use for further scripting, particularly looping through. Needing to figure out this query is a bit of a usability negative, as I do think that there are many reasons one might want to use captured values outside of hurl.

Proposal

I suggest the additon of a -g, --get <VARIABLE> option (or something similar) that effectively just returns whatever using {{variable}} within a hurl script would have gotten at the end of the file. This significantly simplifies the tast of using captured data, and covers the case of wanting a variable from a prior entry before the end that hadn't had its value replaced (which is definitely possible using jq, but I imagine many users wouldn't want to go through the effort of writing it out if there was a simpler option..

As well, I think certain cases such as the data being a single, flat JSON array, could return the contents of the array rather than in JSON format unless --json is still specified. Multiple uses of -g could simply dump values to stdout as-is unless json output is desired, as well.

Additional context and resources

The sw.hurl file is as follows, in case that is helpful:

GET https://www.sunday-webry.com/episode/2550912964453010622
HTTP 200
[Captures]
page: url
json: xpath "string(//*[@id='episode-json']/@data-value)" jsonpath "$.readableProduct.pageStructure.pages[?(@.type=='main')].src"

Tasks to complete

n/a

@Phantop Phantop added the enhancement New feature or request label Aug 31, 2024
@jcamiel
Copy link
Collaborator

jcamiel commented Aug 31, 2024

I agree, a simple way of getting captured variable is a good enhancement. I wonder if, for this use case, we could reuse/extend --write-out option from curl (see for instance #1745).

With curl:

$ curl -w "Server: %header{server}\n" http://example.com will output on stdout the "Server" response header.

We can implement this, plus extend it with Hurl specificity (variables):

$ hurl --write-out "foo %variable{bar}" file.hurl would output "foo" concatenated with the value of the variable ˋbar`

@Phantop
Copy link
Author

Phantop commented Aug 31, 2024

Oooh, that could be a pretty versatile solution. I'd honestly still argue for both ways of doing it, simply because a new user might struggle to find that --write-out option and learning even more additional syntax could be a bit of a pain.

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

No branches or pull requests

2 participants