zweifisch / ob-http

make http request within org-mode babel
GNU General Public License v3.0
256 stars 30 forks source link

Keep trailing newlines in `POST` request #52

Open geolessel opened 3 years ago

geolessel commented 3 years ago

Hi and thanks for the great package!

I've been using org-mode and ob-http to learn Elasticsearch. One of the endpoints allows a submission of Content-Type: application/x-ndjson and requires an final newline in the request body. However, no matter how many newlines I enter into the source block, I keep getting the same error that it needs a trailing newline. It would be great to have an option to retain trailing newlines (or at least add one on execution).

An example of one of my requests:

#+begin_src http
  POST ${host}/_bulk
  Content-Type: application/x-ndjson

  {"create": {"_index": "products", "_id": 201}}
  {"name": "Milk Frother", "price": 149, "in_stock": 14}

#+end_src
eddsteel commented 1 year ago

From the curl manpage:

       -d, --data <data>
              ...
              If  you  start  the data with the letter @, the rest should be a
              file name to read the data from, or - if you want curl  to  read
              the  data  from  stdin.  Posting data from a file named 'foobar'
              would thus be done with -d, --data @foobar. When -d,  --data  is
              told  to  read  from a file like that, carriage returns and new‐
              lines will be stripped out. If you do not want the  @  character
              to have a special interpretation use --data-raw instead.
              ...
       --data-binary <data>
              (HTTP)  This  posts data exactly as specified with no extra pro‐
              cessing whatsoever.

              If you start the data with the letter @, the rest  should  be  a
              filename. Data is posted in a similar manner as -d, --data does,
              except that newlines and carriage returns are preserved and con‐
              versions are never done.

The generated curl command uses -d. I think it would be a safer option to use --data-binary, though it might be nice to allow both with an option.

eddsteel commented 1 year ago

I thought about it and default curl behaviour (as well as the README example) is to assume form data. So I made a PR to add a new option --data-binary. #59