Unable to write variables to .env file
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
env file
I have the same issue like you, have you fixed it?
no it is not fixed
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.
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 :)
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
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.
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!
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