rest.nvim icon indicating copy to clipboard operation
rest.nvim copied to clipboard

Unable to write variables to .env file

Open kalidyasin opened this issue 1 year ago • 7 comments

it runs :REST run successfully but it is not writing the response variable in .env file

POST {{URL}}/api/signin
Content-Type: application/json

{
    "email": "{{USERNAME}}",
    "password": "{{PASSWORD}}"
}
--{%

local body = context.json_decode(context.result.body)

-- These environment variables are stored in 'vim.env'
context.set_env("TOKEN", body._token)
context.set_env("studentid", body.student_id)
context.set_env("certificate", body.certificate_type)
context.set_env("userid", body.user)

--%}

.env file

URL=http://127.0.0.1
[email protected]
PASSWORD=password

http file silicon_20240406_184843 env file silicon_20240406_185024

kalidyasin avatar Apr 04 '24 21:04 kalidyasin

I have the same issue like you, have you fixed it?

harrylowkey avatar Apr 08 '24 04:04 harrylowkey

no it is not fixed

kalidyasin avatar May 01 '24 21:05 kalidyasin

it is described in the test example: https://github.com/rest-nvim/rest.nvim/blob/f96edb54a2940322bc7ed81a1031be04db7d3a99/tests/reqs/script_vars/script_vars.http#L9

it will not save the variable to the file. it will set the variable to vim.env so you can use it.

if you run :lua print(vim.env.userid) after executing the request, it should print whatever comes in the response.

filipiz avatar May 15 '24 19:05 filipiz

Storing variables in an env file was kinda problematic before due to the implementation of it. I'll give it a new life once I get some vacations from exams in the uni and hopefully that one will be good enough :)

NTBBloodbath avatar May 15 '24 23:05 NTBBloodbath

it is described in the test example:

https://github.com/rest-nvim/rest.nvim/blob/f96edb54a2940322bc7ed81a1031be04db7d3a99/tests/reqs/script_vars/script_vars.http#L9

it will not save the variable to the file. it will set the variable to vim.env so you can use it.

if you run :lua print(vim.env.userid) after executing the request, it should print whatever comes in the response.

yes but it is not working if the value is too long like token

kalidyasin avatar May 16 '24 13:05 kalidyasin

It's working for me. I'm storing JWT tokens in my cases, they are quite long. If there is any limitation on nvim.env vars it may be detailed on nvim help files, I guess.

filipiz avatar May 24 '24 23:05 filipiz

I really enjoyed the current solution, @NTBBloodbath. Although I miss the possibility to run a script before the curl request is executed.

I have a use case where I have to generate a "signature" header, that is a hash based on request params and body.

I have hacked the previous version of the plugin to make something similar work to me. On that hack, I set a couple headers like this:

X-Rest-Nvim-Pre-Script: signature.lua X-Rest-Nvim-Post-Script: set_user.lua

and these scripts were executed before and after the request.

Never had the chance to polish and open a pull request. And on this version you have already solved half of the problem.

I'll try to make a pull request as soon as I can find some time to make something useful.

Good luck on your exams!

filipiz avatar May 24 '24 23:05 filipiz

rest.nvim by design won’t touch any .env files. They are always read only.

@filipiz rest.nvim v3 now supports pre-request scripts like intellij does:

@lang lua
< {%
-- scripts
%}
GET http://localhost:3030 HTTP/1.1

boltlessengineer avatar Aug 23 '24 14:08 boltlessengineer