diepm / vim-rest-console

A REST console for Vim.
658 stars 54 forks source link

Body preprocessor option #58

Closed ivangreene closed 6 years ago

ivangreene commented 6 years ago

This PR adds a vrc_body_preprocessor option, which pipes a request body into an external program before sending it in a request. Use case: I prefer to type yaml, as it requires many fewer characters than JSON, and convert it to JSON to send in requests. I would assume others may appreciate this feature.

Would be nice to do, maybe I can add later: syntax highlighting for different body syntaxes. yaml syntax added. Conveniently, json is a subset of yaml so it should appropriately highlight all.

diepm commented 6 years ago

Thanks for your PR. It will help speed up dev time a lot. Let me play around with it a bit. Will get back to you shortly.

ivangreene commented 6 years ago

Thanks for the nice plugin! It has already sped up my time working with APIs this week.

A couple things in this PR that still need work:

diepm commented 6 years ago

I try this request (on mac) but it fails. Probably I did something wrong.

http://localhost:9200
POST /testindex/testtype
---
  key: k
  value: v

The error message (extracted from the stack trace of yaml2json) is Error: expected indent after document, near "^M key: k^M value: v". The data body before join() looks ok.

['---', ' key: k', ' value: v']

I guess it's something related to the newline character in

let dataLines = systemlist(preproc, join(a:request.dataBody, "\r"))

ivangreene commented 6 years ago

Hmm... I'm using Mac as well, haven't run in to that. ^M I believe is a carriage return, not sure why yaml2json is choking on that. Which version/dist of Vim are you using? I tried replacing \r with \n which seems to work also. That may fix this problem for you

diepm commented 6 years ago

Is it possibly due to the yaml2json I'm using (installed by npm install yaml2json)?

ivangreene commented 6 years ago

That may be it; I'm using one from pip. I used \r because I was under the impression that was the system-independent way to write newline in VimScript, finding that may not be true if it's giving a carriage return instead.

diepm commented 6 years ago

Let me try from pip. If it works, then we can note this issue in the docs.

diepm commented 6 years ago

Python one works including the script I wrote by hand. NPM's yaml2json doesn't properly handle the newline character.

diepm commented 6 years ago

What do you mean by the second TODO item?

Need to figure out how GET queries should be handled (not preprocessed at all, optionally preprocessed?)

ivangreene commented 6 years ago

I figure we may want to leave a GET query that is urlencoded as-is because it’s a different format than any of the other bodies

diepm commented 6 years ago

What do you think if we apply the preprocessor for GET only when vrc_allow_get_request_body is enabled? Since if this option is on, the body will be sent as a whole using --data.

ivangreene commented 6 years ago

Just added a commit that does that, as well as avoids running @filename bodies through the preprocessor. Seems like the most reasonable balance to me.

diepm commented 6 years ago

Thanks for this great feature. I'll update the docs accordingly.