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

Variable replacement in file bodies #1116

Open
tpluscode opened this issue Dec 19, 2022 · 7 comments
Open

Variable replacement in file bodies #1116

tpluscode opened this issue Dec 19, 2022 · 7 comments
Labels
enhancement New feature or request topic: generators

Comments

@tpluscode
Copy link

Problem to solve

I prefer to set the request body from a file to keep IDE's syntax support applicable to the given file type

Unfortunately, the {{variable}} replacement tokens do not work there

Proposal

Given that some file types may use the double brace for its own purposes, I'd suggest a new option which would opt-in to replace {{foobar}} as it would when the body was inline in a .hurl file

Request file:

POST /endpoint
file,body.json;
[Options]
replace-file-variables: true

body.json:

{
  "foo": "{{bar}}"
}

When called like hurl --variable bar=baz, it should replace the {{bar}} token in the JSON file before sending the request.

Might also consider a CLI switch --replace-file-variables

@tpluscode tpluscode added the enhancement New feature or request label Dec 19, 2022
@fabricereix
Copy link
Collaborator

It could be indeed useful. But we must be clear whether the content is binary or text.
Currently, a template in Hurl is always text while an external file body are bytes.

@deicon
Copy link

deicon commented Dec 20, 2022

I like the idea and started looking into it. I wonder why hurl is not using a mustache based templating system like https://github.com/nickel-org/rust-mustache.

@fabricereix
Copy link
Collaborator

We haven't looked specifically at this crate. We understand that using an external crate is attractive. But generally, we want to minimize external dependencies and have a good/consistent error reporting within Hurl. For the time-being, it was easier to do with internal code.

@deicon
Copy link

deicon commented Dec 21, 2022

This sounds reasonable. So I would work on this using the existing templating as it is.
How about adding a tag in addition to file to indicate, that the file contains a template ?

POST /endpoint
file-template,body.json;application/json
{
  "foo": "{{bar}}"
}

@jcamiel
Copy link
Collaborator

jcamiel commented Dec 21, 2022

I’m not sure we’re ready to develop the feature, I prefer we discuss the potential syntaxes before.

For instance, we plan to have support for « generator » in template:

POST https://foo.com
```
{{ now }}
```

To create a body with current time. This kind of generator could be used for providing a templatized body :

POST https://foo.com
```
{{ template data.txt }}
```

You can work on it, but not I’m not convinced by file-template syntax.

@deicon
Copy link

deicon commented Dec 21, 2022

Looking at the generator feature, this is basically what the mustache crate brings on the table by allowing closures to be bound to the template context. Which is the reason I was asking this in the first place. If hurl would parse the inline body using the mustache template crate, the generator feature would be a matter of adding closures like

template
now
etc.

or at least this would be a start.

@fabricereix
Copy link
Collaborator

Discussed in #2713
reusing the file keyword with an additional template attribute

file,body.json;application/json;template=true

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: generators
Projects
None yet
Development

No branches or pull requests

4 participants