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

An easy way to reuse requests #317

Open
jcamiel opened this issue Oct 22, 2021 Discussed in #274 · 11 comments
Open

An easy way to reuse requests #317

jcamiel opened this issue Oct 22, 2021 Discussed in #274 · 11 comments
Labels
enhancement New feature or request topic: import

Comments

@jcamiel
Copy link
Collaborator

jcamiel commented Oct 22, 2021

(Imported from original discussion @spaceaardvark)

I don't think there's a way to do this yet, but it would be great to have an easy way to reuse a request in multiple .hurl files.

For example, suppose sign-in.hurl signs in a test user in and extracts their name and email from the response. I would like to reuse that request in test-1.hurl and test-2.hurl without having two duplicate everything in sign-in.hurl.

@fabricereix
Copy link
Collaborator

That could be useful but we are also afraid that it will bring a lot of other issues: error reporting, imports cycles,...

For the time-being, we recommend to generate the Hurl files (with scripts) when we need to reuse common requests.
But we understand that it not might be the best for everyone.
We still need to think about it.

@nikeee
Copy link
Contributor

nikeee commented Jan 21, 2022

Do hurl files share the same session when passed as a parameter? If so, one could just do hurl login.hurl action.hurl, with login.hurl capturing the necessary tokens.

@fabricereix
Copy link
Collaborator

Each Hurl file has each own session.
You could use the standard input to get the same session

cat login.hurl action.hurl | hurl

@lepapareil
Copy link
Collaborator

@nikeee perhaps this #115 will meet your needs :).

@jcamiel jcamiel mentioned this issue Feb 15, 2023
4 tasks
@MissingHandle
Copy link

we are also afraid that it will bring a lot of other issues

the sign of a successful project?

@nikeee
Copy link
Contributor

nikeee commented Oct 10, 2023

For the time-being, we recommend to generate the Hurl files (with scripts) when we need to reuse common requests.

Today, I've refactored our scripts that generate the hurl files. Before, it was a bash script that outputs hurl contents (by using cat on the parts). Didn't like that approach.

Instead, I've refactored it to use a "preprocessor" that replaces #include directives with the respective file contents. It also resolves symlinks and detects cyclic dependencies. If anyone needs it, you can find it here:
https://gist.github.com/nikeee/748301705d3ed53b6b8776fba8748a9b

The file sill has valid hurl syntax, because # is also the start of a comment. So no syntax highlighting is broken:

#include actions/login-user.hurl

POST http://localhost:8080/contact
[FormParams]
email: someone@example.com
subject: Test
token: {{xsrf-token}} # from login-user.hurl

This seems to work pretty good for us at the moment.

@fabricereix
Copy link
Collaborator

Thanks @nikeee for sharing!
it's very clean and readable.
I think it will be interesting to add it in our contrib folder.

@nikeee
Copy link
Contributor

nikeee commented Oct 11, 2023

If this is something you'd be interested in, I'd file a PR for the contrib folder (and make it a bit more resistent; for example, trailing whitespace does not work yet).


While implementing and using that, I realized that some users might want to add parameters to scripts or override variables respectively.

For example, you can call hurl and set a variable that is used in the hurl file:

hurl --variable host="example.com" a.hurl

THis might be useful when including/re-using hurl files. I modeled a workflow where there are two users, with two different sessions. Depending on the action included, it might make sense to override variables in the includes:

POST /login
[Captures]
user_cookie: ...

POST /admin-login
[Captures]
admin_cookie: ...

#include post-comment.hurl --variable cookie={{user_cookie}}

#include delete-comment.hurl --variable cookie={{admin_cookie}}

(the syntax is only for demonstration of the idea, not an actual proposal)

This is not supported in the preprocessor script above as I managed to get around the need for that (yet).

@fabricereix
Copy link
Collaborator

Yes, you can file a PR in contrib/preprocessing folder.
We like generating source files in general. We treat the generated sources as normal source files, namely committed in the repository. With Hurl, they will be the one to look at when an error occurs.
Do you also commit them?

@rochacbruno
Copy link

Duplicate #2110

@dodalovicgran
Copy link

Each Hurl file has each own session. You could use the standard input to get the same session

cat login.hurl action.hurl | hurl

While this works, once you get an error, you have no clue where it comes from.

@jcamiel jcamiel added enhancement New feature or request topic: import labels May 22, 2024
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: import
Projects
None yet
Development

No branches or pull requests

7 participants