AnWeber / httpbook

Quickly and easily send REST, Soap, GraphQL, GRPC, MQTT and WebSocket requests directly within Visual Studio Code
https://httpyac.github.io/
MIT License
57 stars 4 forks source link

Variables do not carry over between cells #102

Closed patrickhuy closed 8 months ago

patrickhuy commented 9 months ago

When working with variables like this

{{
  exports.foo = 'bar';
}}
GET https://google.de/?q={{foo}}
###
GET https://google.de/?q={{foo}}

The second request will fail with

ReferenceError: foo is not defined

For unknown reasons I had it randomly work but most of the time it does not work. In httpyac it works fine.

AnWeber commented 9 months ago

That's actually intentional. I separate the regions that these do not share the variables. Exception is when multiple cells are executed at once, then the variable scope is not always reset, but that could be explicitly enabled. You should use global regions (region with no name and no request) for your case.

{{
  exports.foo = 'bar';
}}
###
GET https://google.de/?q={{foo}}
###
GET https://google.de/?q={{foo}}

The idea behind this is that all regions can always be executed independently. If they need variables from other regions, these should be referenced explicitly using @ref.

patrickhuy commented 8 months ago

Mh, the original use-case is/was to set variables in "post-request" scripts based on the API response with a "m-n" structure. So:

# create job using method A
POST /job
{{
  exports.id=1;
}}
###
# create job using method B
POST /job
{{
  exports.id=2;
}}
###
# get job status
GET /job/{{id}}
###
# delete job
DELETE /job/{{id}}

the intention is that one of the "create job" methods will be used (not nescessarily both or in that order) but the id of either would become available in "get job status". I therefore can not @ref the "correct" one.

In the "httpYac" extension this also works fine as is. In "httpBook" it does not. Does useRegionScopedVariables not default to false? I also tried setting it to false but it did not change anything. Setting it to true did make this no longer work in httpYac.

AnWeber commented 8 months ago

vscode-httpyac and httpbook may differ from each other in detail here. The reason is that each cell is considered as a separate file by vscode and I create a virtual httpFile but I am missing some details like the scoped variable store. ... Ok yeah you got me. I will align both approaches.

AnWeber commented 8 months ago

Your example should work with the new version v6.2.3

patrickhuy commented 8 months ago

Can confirm, it works nicely! :)

AnWeber commented 8 months ago

thx for your feedback.-)