restclient.el icon indicating copy to clipboard operation
restclient.el copied to clipboard

Get content from file for HTTP request body.

Open alexei-28 opened this issue 8 years ago • 7 comments

My text.rc file

:REQUEST_BODY = <<
{
  "id": 1,
  "name": "company  Name"
}


# Update company's name
POST :API_TEST/updateCompany

:REQUEST_BODY

And it's HTTP request work fine. OK.

Question: Can I get content for variable REQUEST_BODY from file?

I want something like this:

:REQUEST_BODY = /path/to/file

Is it possible?

alexei-28 avatar Sep 13 '17 15:09 alexei-28

You can do something like this:

:token := (with-temp-buffer (insert-file-contents "token") (buffer-string))

TatriX avatar Sep 19 '17 08:09 TatriX

Thanks. It's work. Here my example:

:REQ_BODY := (with-temp-buffer (insert-file-contents "d:/test/company/my.json") (buffer-string))

But has another problem. I want to extract folder path as another varable. And I want to concatenate folder path and file name.

Here snippet:

:DIR_PREFIX = "d:\\test\\company"
:REQ_BODY := (with-temp-buffer (insert-file-contents (concat ":DIR_PREFIX" "my.json")) (buffer-string))

But I get error:

Opening input file: Invalid argument, d:/test/company/:DIR_PREFIXmy.json

alexei-28 avatar Sep 21 '17 13:09 alexei-28

You can use something like this:

:ignore := (setq my-dir-prefix "/path/do/dir")
:DIR_PREFIX := my-dir-prefix
:REQ_BODY := (concat my-dir-prefix "my.json")

TatriX avatar Sep 21 '17 17:09 TatriX

Thanks. Here work code:

:ignore := (setq dir-prefix-company "d:/tests/company/json")
:REQ_BODY := (with-temp-buffer (insert-file-contents (concat (file-name-as-directory dir-prefix-company) "my.json")) (buffer-string))

I don't know about :ignore. In description of package restclient I'm not found anything about :ignore. Where you found information about :ignore? Thank you!

alexei-28 avatar Sep 21 '17 17:09 alexei-28

:ignore is a randomly chosen name which is used to apply side effects. You can call it whatever you want.

TatriX avatar Sep 21 '17 18:09 TatriX

I found in description what are you talking about:

There's no way to reference earlier declared restclient variables, but you can always use setq to save state

Now, I understand how it's work. Thank you!

alexei-28 avatar Sep 21 '17 18:09 alexei-28

if it's able to write some lisp code right after request code block to handle response content would be great.

gravitywp avatar Aug 28 '19 07:08 gravitywp