[racket-users] Re: Sending a file via HTML request.
On Tue, 23 Aug 2016 02:09:24 -0700 (PDT), Normal Loone 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 overhead may be significant. >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 I understand correctly, you're trying to send the file to a server via HTTP from a Racket client. If the file is JSON encoded, you can send it as a simple HTTP parameter: e.g., (require net/http-client net/uri-codec) (let [ (params (alist->form-urlencoded (list (cons 'file (write-bytes your-file-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)) )) Sending binary file data or extremely long files (that can't be loaded fully into memory) requires an HTTP multipart form. 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.
Re: [racket-users] Re: Sending a file via HTML request.
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 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.
Re: [racket-users] Re: Sending a file via HTML request.
Vishesh Yadav wrote a DrRacket plugin that automatically compiles a program to JS via whalesong and uploads it to a server. It sounds related to what you are describing so you may want to take a look at his code. The plugin code is here: https://github.com/vishesh/drracket-whalesong in particular these lines may be of interest: https://github.com/vishesh/drracket-whalesong/blob/master/tool.rkt#L62-L74 and the server code is here: https://github.com/vishesh/whalebin If you want to try it out, the server is running at bigbang.ccs.neu.edu (but seems to be experiencing temporary problems at the moment). On Wed, Aug 24, 2016 at 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. > > -- > 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. -- 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.
[racket-users] Re: Sending a file via HTML request.
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. -- 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.
[racket-users] Re: Sending a file via HTML request.
You could base64 encode the file bytes and stick that into a field in the JSON you send. Why not just send the file directly though? For instance, instead of sending this: POST /somewhere Content-Type: application/json { filename: "foo" content: "Zm9vLGJhcixiYXoNCjEsMSwxDQoyLDIsMg0KMywzLDM=" } Send this: POST /somewhere Content-Type: text/csv foo,bar,baz 1,1,1 2,2,2 3,3,3 -- 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.