Re: [racket-users] Sending a file via HTML request.

2016-08-24 Thread 'John Clements' via Racket Users

> On Aug 24, 2016, at 4: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.

Yes, this is a one-liner:

#lang racket

(require net/http-client)

(http-sendrecv #"example.com"
   #"/"
   #:method "POST"
   #:data "MY FAVORITE BYTES”)

This will send a POST request to example.com, with path /, and attach the bytes 
#”MY FAVORITE BYTES”.

John Clements


-- 
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.


signature.asc
Description: PGP signature


Re: [racket-users] Sending a file via HTML request.

2016-08-23 Thread spdegabrielle
Hi, 

JSON (require json-parsing) see 
https://docs.racket-lang.org/json-parsing/index.html 

I've cribbed the HTML below from 
http://www.w3schools.com/tags/att_input_accept.asp 


On the web server side this SO response seems to answer, 

http://stackoverflow.com/questions/20588641/in-racket-how-do-you-allow-file-upload-to-the-web-server

#lang web-server/insta
(require web-server/formlets)

 ; start: request -> doesn't return
(define (start request)
  (show-page request))

; show-page: request -> doesn't return
(define (show-page request)
  ; Response generator
  (define (response-generator embed/url)
(response/xexpr
 `(html 
   (head (title "File upload example"))
   (body (h1 "File upload example"))
   (form 
([action ,(embed/url upload-handler)]
 [method "POST"]
 [enctype "multipart/form-data"])
,@(formlet-display file-upload-formlet)
(input ([type "submit"] [value "Upload"]))

  (define (upload-handler request)
(define-values (fname fcontents)
  (formlet-process file-upload-formlet request))
(define save-name (string-append "!uploaded-" fname))
(current-directory WORKINGDIR)
(display-to-file fcontents save-name #:exists 'replace)
(response/xexpr
 `(html
   (head (title "File Uploaded"))
   (body (h2 "File uploaded")
 (p ,fname)
 (h2 "File size (bytes)")
 (p ,(number->string (file-size save-name)))

  (send/suspend/dispatch response-generator))


; file-upload-formlet: formlet (binding?)
(define file-upload-formlet
  (formlet
   (div ,{(file-upload) . => . binds})
   ; (formlet-process file-upload-formlet request)
   ; returns the file name and contents:
   (let
   ([fname (bytes->string/utf-8 (binding:file-filename binds))]
[fcontents (binding:file-content binds)])
 (values fname fcontents


(I've not tried this - I'm not sure it is correct)








  
  


Note: The accept attribute of the input tag is not 
supported in Internet Explorer 9 (and earlier versions), and Safari 5 (and 
earlier).
Note: Because of security issues, this example will not 
allow you to upload files.





HTH Stephen


On Tuesday, August 23, 2016 at 3:47:05 PM UTC+1, Normal Loone wrote:
> No, I don't have it in JSON yet. How do I load a file into JSON in racket?
> 
> It's only a text file, although I like to get it zipped first.

-- 
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] Sending a file via HTML request.

2016-08-23 Thread Normal Loone
No, I don't have it in JSON yet. How do I load a file into JSON in racket?

It's only a text file, although I like to get it zipped first.

-- 
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] Sending a file via HTML request.

2016-08-23 Thread Matthias Felleisen

If the file is in JSON, just read-json and then write-json to the port. 


> On Aug 23, 2016, at 5:09 AM, Normal Loone  wrote:
> 
> Yeah, my main problem is how do I pack the file into a JSON Object and then 
> send it?
> 
> 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?
> 
> -- 
> 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.


Re: [racket-users] Sending a file via HTML request.

2016-08-23 Thread Normal Loone
Yeah, my main problem is how do I pack the file into a JSON Object and then 
send it?

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?

-- 
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] Sending a file via HTML request.

2016-08-22 Thread 'John Clements' via Racket Users

> On Aug 21, 2016, at 2:32 AM, Normal Loone  wrote:
> 
> Hallo,
> 
> I am trying to write a small web-based program in racket. The main task of it 
> should be to send a specific file to a server. 
> 
> How do I best send it? Through a HTML request or on another way?

Your use of the term “web-based” is a teeny bit confusing to me. You might be 
describing one of two things:

1) a Racket program is running on the client and a Racket program is running on 
the server, or
2) the client is using a web browser and a Racket program is running on the 
server.

In either case, the server program is probably going to be a simple one that 
responds to a request that contains
the file as the request content. The filename could be part of the URL’s path, 
or you could bundle it with the file and
pass the whole thing as a JSON object. 

John Clements



-- 
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] Sending a file via HTML request.

2016-08-21 Thread Normal Loone
Hallo,

I am trying to write a small web-based program in racket. The main task of it 
should be to send a specific file to a server. 

How do I best send it? Through a HTML request or on another way?

-- 
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.