AnWeber / vscode-httpyac

Quickly and easily send REST, Soap, GraphQL, GRPC, MQTT and WebSocket requests directly within Visual Studio Code
https://marketplace.visualstudio.com/items?itemName=anweber.vscode-httpyac
MIT License
237 stars 20 forks source link

Is there a way to skip variable substitution inside variable data? #181

Closed dremekie closed 1 year ago

dremekie commented 1 year ago
{{
  const fs = require('fs');
  $global.myJsonFile = JSON.parse(fs.readFileSync('myJsonFile.json', 'utf8'));
  exports.myJsonFile = JSON.parse(fs.readFileSync('myJsonFile.json', 'utf8'));.
}}

POST {{gatewayUrl}}/saveConfig
Content-Type: application/json

{
  "config": {{$global.myJsonFile}}
}

POST {{gatewayUrl}}/saveConfig
Content-Type: application/json

{
  "config": {{myJsonFile}}
}

Any of the above POSTs result in ERROR: ReferenceError - title is not defined.

myJsonFile.json is a JSON document that contains strings with {{title}} in it as well as many other strings with {{xxx}} that should be POSTed to the endpoing as-is. I cannot escape them with \{\{ because they are needed as-is by other processes.

Is there a way to NOT perform variable substitution within the actual variable data (i.e.myJsonFile) ?

AnWeber commented 1 year ago

@dremekie The easiest way would be to import the file directly as a body. In this case the file is only read in as a buffer and not modified (config property should be added to json file)

POST {{gatewayUrl}}/saveConfig
Content-Type: application/json

< ./myJsonFile.json

Another solution would be to use the escaped variant \{\{...\}\} instead of {...}} in the template string. This will then be automatically replaced correctly (see example)

Another possibility would be to set the body directly as a buffer. The body would no longer be adjusted and sent.

{{
  const fs = require('fs');
  request.body = Buffer.from(`{ "config": ${fs.readFileSync('myJsonFile.json', 'utf8')}`);
}}

POST {{gatewayUrl}}/saveConfig
Content-Type: application/json
dremekie commented 1 year ago

Thanks