vscode-restclient icon indicating copy to clipboard operation
vscode-restclient copied to clipboard

Multi-line variables

Open Marvin-Brouwer opened this issue 4 years ago • 7 comments

This might be a very niché request but it also might not be. I am in a situation where I need to talk to an API that accepts JSON inside of a form-encoded field

POST https://{{apiHost}}/path HTTP/1.1
Host: {{apiHost}}
Content-Type: application/x-www-form-urlencoded
Cache-Control: no-cache

&value=test
&attributes={ "some attribute here": "some value here" }

It's not the prettiest of API's but it's what we have to work with, the list of attributes can get quite large and I'd like to assign this to a variable with formatted json but put it inside of the form-encoded field like so:

@attributes = { 
  "some attribute here": "some value here",
  "another attribute": 4
}

POST https://{{apiHost}}/path HTTP/1.1
Host: {{apiHost}}
Content-Type: application/x-www-form-urlencoded
Cache-Control: no-cache

&value=test
&attributes={{attributes}}

I'm not sure what would be the best approach I was either thinking about wrapping it in curly braces or using a similar approach to Powershell where you'd use a backtick to allow a new line for single-line statements.

Marvin-Brouwer avatar Oct 27 '20 12:10 Marvin-Brouwer

@Marvin-Brouwer this is not supported yet, but nice suggestion

Huachao avatar Oct 29 '20 11:10 Huachao

I think to support multi line variables, the parser needs to know a start and end-tag that would not be part of the content. Like the three single/double quotes in python.

cplussharp avatar Apr 06 '21 10:04 cplussharp

I've just found myself in a similar need. How about using ``` just like markdown to flag beginning and end of variable content:

@jsonvar = ```
{
    "id": 123, 
    "some": "val"
} 
    ```

gregid avatar Apr 12 '21 12:04 gregid

In the vim client the syntax is like this:

:header = <<
Content-Type: application/json
Authorization: key

Just for reference. It might be an idea to use this?

eikooc avatar Jul 27 '22 09:07 eikooc

I would love this too.

My first thought was the triple ticks or <<

@cartFragment <<END
fragment cartFields on Cart {
  id
  createdAt
  updatedAt
}
END

rsperko avatar Feb 02 '23 01:02 rsperko

How about just allowing JS execution for a var?

@variableName = {{ $call: './getPayload.js' }}

That way you can just return an object from js, you could even possibly support ts, json, or yaml. And as an added bonus that would allow you to call a secret store for variables.

Marvin-Brouwer avatar Feb 02 '23 12:02 Marvin-Brouwer

Any news or plans about this, this is something that would prove to be very useful

asos-gurpreetsingh avatar Feb 29 '24 11:02 asos-gurpreetsingh