tkf / emacs-request

Request.el -- Easy HTTP request for Emacs Lisp
http://tkf.github.com/emacs-request/
GNU General Public License v3.0
629 stars 93 forks source link

Response streaming? #225

Open CyberShadow opened 8 months ago

CyberShadow commented 8 months ago

Hi,

Is it possible to receive response data incrementally? E.g. for Content-Type: text/event-stream responses?

I kinda managed to get it to work with url with an after-change-functions hook, but that seems hacky and there doesn't seem to be a clean way to interrupt an in-progress request.

Thanks!

dickmao commented 8 months ago

I kinda managed to get it to work with url with an after-change-functions hook,

You mean like this?

(let ((b (url-retrieve "https://httpbin.org/range/1024?duration=8&chunk_size=64"
                       (lambda (&rest _args) (kill-buffer)))))
  (push (lambda (beg end _len)
      (message "Got %s" (buffer-substring beg end)))
    (buffer-local-value 'after-change-functions b)))

I wouldn't use this library anymore. It's slow and more trouble than it's worth. Granted the built-in url library is written in precisely the wrong way, but I still prefer it to clumsily spawning a curl subprocess.

CyberShadow commented 8 months ago

You mean like this?

Yes, that's what I'm doing. Aside from the problem of how to disconnect, the above code is not quite correct when something like "Content-Encoding: chunked" is used. But if you stick the hook in a 0-second timer so that it runs after url does its postprocessing, then it seems to work fine.

I wouldn't use this library anymore.

Sorry, do you mean url or request?

Either way, what would you recommend?

dickmao commented 8 months ago

I wouldn't use request. It sucks, and while Content-Encoding: chunked, whatever that means, doesn't work out of the box with url, I wouldn't attempt to get it to work with request. I also gag slightly when I hear about 0-second timers. Sorry bruh. Will accept crypto if you want me to do it right nice. Problem statement must be terse and limited to 500 words including code snippets.

CyberShadow commented 8 months ago

Err, thanks, writing code is not the problem. It's all going up on GitHub in the end anyway. Was just wondering if there's a more established library I should be using instead. Cheers.

dickmao commented 8 months ago

I believe some nimrod is floating his own http library as an alternative to this one which he denigrates every chance he gets. Google around.