I posted to this thread yesterday through Gmane, but I see it hasn't yet appeared. Posting through Gmane has been flaky lately - I'm sending this updated version via email. Not sure what's happening with Gmane. Apologies if you see things multiple times.
George ------------------------------------------------------------------------ On 8/24/2016 7:36 AM, Normal Loone wrote:
Sorry, I should have been clearer: I want to send a file directly from DrRacket to a server. I have submit button as a plugin in DrRacket and it then should take the file and send it to the server (the file is known, doesnt need to be selected from user). I tried the code from HTH Stephen, but problem is that the web application starts on DrRacket start and not when I press the button, how I wanted it. So if someone could tell me a way to just directly send a file with DrRacket as an HTML request, I'd really appreciate that.
On Tue, 23 Aug 2016 02:09:24 -0700 (PDT), Normal Loone <andreas.m.wiest-mmb7mzph...@public.gmane.org> wrote: >Yeah, my main problem is how do I pack the file into a JSON Object >and then send it? Just FYI: JSON is a printable coding, so any non-printable characters in a binary file must be escaped - making the transfer larger. The read-json and write-json functions take care of escaping, but if you are sending large files, the conversion overhead may be significant. Note that converting an arbitrary binary file to JSON is convoluted - JSON is meant to handle character strings, not bytes. You have to do something like: (string->jsexpr (bytes->string/utf-8 file-bytes)) which is a problem if the file is very large as the file data and converted string both need to be in memory. It's possible to send a file in pieces and reassemble it at the receiver, but that can get quite complicated. >With (require net/http-client) I could already establish a connection >with (http-conn-sendrecv! hc uri), but All I can send is the empty >header of the http request. > >How do I add the HTML code or the file in the json object within it? If the data is JSON encoded, you can send it as an HTTP string parameter: e.g., (require net/http-client net/uri-codec) (let [ (params (alist->form-urlencoded (list (cons 'file json-encoded-data )) ))) (headers (list "Content-Type: application/x-www-form-urlencoded") ) ] (let-values [ ((resp-code resp-hdrs port) (http-sendrecv host url #:method #"POST" #:data params #:headers headers )) ] (printf "~s~n~s~n~s~n" resp-code resp-hdrs (port->bytes port)) )) The print at the end is displaying the server's response. There is a limit - which I can't recall at the moment - to the size of simple upload forms, so you might have trouble sending a long file this way. Large uploads, binary data uploads, and piecemeal incremental uploads are meant to be done using multipart forms. Multipart forms are complex to encode: they have opening headers, boundary markers enclosing and sometimes interspersed with the data, and closing footers after the data. I don't have a ready example. For more information see http://www.rfc-base.org/rfc-1867.html Hope this helps, George -- You received this message because you are subscribed to the Google Groups "Racket Users" group. To unsubscribe from this group and stop receiving emails from it, send an email to racket-users+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.