hurl icon indicating copy to clipboard operation
hurl copied to clipboard

Variable replacement in file bodies

Open tpluscode opened this issue 3 years ago • 8 comments

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 avatar Dec 19 '22 09:12 tpluscode

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.

fabricereix avatar Dec 19 '22 16:12 fabricereix

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.

deicon avatar Dec 20 '22 19:12 deicon

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.

fabricereix avatar Dec 21 '22 17:12 fabricereix

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}}"
}

deicon avatar Dec 21 '22 17:12 deicon

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.

jcamiel avatar Dec 21 '22 18:12 jcamiel

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.

deicon avatar Dec 21 '22 18:12 deicon

Discussed in https://github.com/Orange-OpenSource/hurl/discussions/2713 reusing the file keyword with an additional template attribute

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

fabricereix avatar Apr 23 '24 08:04 fabricereix