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

Feature request: File variables based on request variables #181

Closed
lldata opened this issue Apr 3, 2018 · 13 comments
Closed

Feature request: File variables based on request variables #181

lldata opened this issue Apr 3, 2018 · 13 comments

Comments

@lldata
Copy link

lldata commented Apr 3, 2018

The new feature where you can use information from the response in the following request is very nice. But it could be improved. This is the current version:

# @name createComment
POST {{baseUrl}}/comments HTTP/1.1
Authorization: {{login.response.headers.X-AuthToken}}
Content-Type: application/json

{
    "content": "fake content"
}

###

# @name getCreatedComment
GET {{baseUrl}}/comments/{{createComment.response.body.$.id}} HTTP/1.1
Authorization: {{login.response.headers.X-AuthToken}}

I'd like if you could define a variable, with data captured from the response.
Like this:

# @name createComment
POST {{baseUrl}}/comments HTTP/1.1
Authorization: {{login.response.headers.X-AuthToken}}
Content-Type: application/json

{
    "content": "fake content"
}

###

@commentId={{createComment.response.body.$.id}}

# @name getCreatedComment
GET {{baseUrl}}/comments/{{commentId}} HTTP/1.1
Authorization: {{login.response.headers.X-AuthToken}}

Currently @commentId will have the value "{{createComment.response.body.$.id}}"
I'd expect the second example to do the same as the first.

@lldata
Copy link
Author

lldata commented Apr 3, 2018

Just realized that my request looks a bit like #179
The difference is in the syntax on how you override a variable.

I'd say that the least surprising syntax is with the @ prefix ...

@Huachao
Copy link
Owner

Huachao commented Apr 4, 2018

@lldata nice suggestion, it's different from that.

@Huachao Huachao changed the title Feature request: Variables based on responses Feature request: File variables based on request variables Apr 8, 2018
@lldata
Copy link
Author

lldata commented Apr 12, 2018

You could avoid holding all responses in memory, if you allowed a section below the request, that could post process the response. Like this:

# @name postjson
POST http://httpbin.org/post

{
    "key1" : "a",
    "key2" : "b"
}

@@@
# post process response (@@@ is new syntax)

# capture response to environment variables
@key1={{postjson.response.body.json.key1}}
# no need to name response in this section
@key2={{response.body.json.key2}}

# other special syntax could also be possible in this section
# e.g. save response to file (opposite syntax or reading file for request)
> ./save.response.in.file.json

# run script to post process response
@run=./postprocess.ts 

###
# 2nd request that uses captured variables
GET http://httpbin.org/get?key1={{key1}}&key2={{key2}}

Hope you get the idea.

@Huachao
Copy link
Owner

Huachao commented Apr 12, 2018

@lldata nice suggestion, script support is in my consideration, I'd like to provide some hooks(before request and after response) for user to write customize script. Add user just need to specify the script location in the request metadata like existing name metadata. For examle:

# @name sample
# @script beforeRequest ./test1.js
# @script afterResponse ./test2.js
GET https://api.example.com

@lldata
Copy link
Author

lldata commented Apr 12, 2018

I can see how you can support scripts in the comment section above the request.

Would that work for my two other examples as well?

  • capture response to variable
  • save response to file

You are getting into the developing a DSL for testing HTTP. It is very useful and if you get it right, I could imagine that other tools could pick the format up and use it as well!

@eamodio
Copy link

eamodio commented Apr 23, 2018

I would also love the ability to set a variable from the response: e.g. @foo = {{login.response.body.$}}

That way I can have File level variables that can be overwritten by a request, and to allow a simple naming structure to more complex jsonpath queries

@Huachao
Copy link
Owner

Huachao commented Apr 23, 2018

@eamodio this feature is in my todo list. And I am still considering the design.

@Huachao
Copy link
Owner

Huachao commented Sep 20, 2018

@lldata @dakaraphi @eamodio @ReeganExE @itgoran I have implemented this feature and will publish in next release. The syntax would be the same as you suggested, like @foo = {{login.response.body.$}}, and you may even mix the plain text and multiple request variable references like @foo = {{login.response.body.id}}JOIN{{login.response.body.token}}

@Huachao Huachao closed this as completed Sep 20, 2018
@Huachao
Copy link
Owner

Huachao commented Oct 8, 2018

@lldata @dakaraphi @eamodio @ReeganExE @itgoran you can try the latest version 0.20.0 to verify this feature.

@lldata
Copy link
Author

lldata commented Oct 9, 2018

It works, but I could get it a little confused if I by mistake named a variable the same as a request:

# @name postjson
POST http://httpbin.org/post

{
    "key1" : "a",
    "key2" : "b"
}

###
@key1={{postjson.response.body.json.key1}}
@postjson={{postjson.response.body.json}}

###
# 2nd request that uses captured variables
GET http://httpbin.org/get?key1={{key1}}

###
POST http://httpbin.org/post

{{postjson}}

To reproduce run postjson request - mouseover on postjson variable in bottom of file.
The editor resolves the content of postjson correctly, but also gives a warning.

image

@Huachao
Copy link
Owner

Huachao commented Oct 9, 2018

@lldata as I've mentioned in the README, the same name request variable has higher precedence over the same name file variables. So when sending request, it treats the {{postjson}} as a request variable. And the reason why {{postjson}} displays the correct request variable value is that our extension will fetch the variable value concurrently both as a request variable, file variable, and an environment. So you'd better give the file variable another name.

@pungggi
Copy link

pungggi commented Oct 10, 2018

Nice Feature Huachao very nice!

@georgepaoli
Copy link

# @name sample
# @script beforeRequest ./test1.js
# @script afterResponse ./test2.js
GET https://api.example.com

@Huachao , script support has available?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants