読者です 読者をやめる 読者になる 読者になる

IT練習ノート

IT関連で調べたこと(実際は嵌ったこと)を書いています。

ServantのFileUploadサンプル

package

servant-multipart: multipart/form-data (e.g file upload) support for servant

$ cabal install servant-multipart 
Resolving dependencies...
Notice: installing into a sandbox located at
foo/.cabal-sandbox
Downloading http-client-0.5.6.1...
Configuring natural-transformation-0.4...
Configuring http-client-0.5.6.1...
Building natural-transformation-0.4...

...

sample

servant file upload sample

client

$ curl -i -X PUT http://127.0.0.1:8081/file -F "upfile1=@MyWebApp_upload.hs" -F "xx=yy"
HTTP/1.1 100 Continue

HTTP/1.1 200 OK
Transfer-Encoding: chunked
Date: Tue, 14 Mar 2017 12:03:05 GMT
Server: Warp/3.2.9
Content-Type: application/json;charset=utf-8

{"msg":"\"upfile doesn't exist\"","len":-1}
$
$ curl -i -X PUT http://127.0.0.1:8081/file -F "upfile=@MyWebApp_upload.hs" -F "xx=yy"
HTTP/1.1 100 Continue

HTTP/1.1 200 OK
Transfer-Encoding: chunked
Date: Tue, 14 Mar 2017 12:03:13 GMT
Server: Warp/3.2.9
Content-Type: application/json;charset=utf-8

{"msg":"uploaded","len":1633}
$ ls -al MyWebApp_upload.hs 
-rw-r--r--  1 foo bar  1633  3 4 11:01 MyWebApp_upload.hs
$ 

server log

*Main> main
FileData {fdInputName = "upfile1", fdFileName = "MyWebApp_upload.hs", fdFileCType = "application/octet-stream", fdFilePath = "/var/folders/hw/p4bp49hd7v9_1j60sjvmhqnr0000gn/T/servant-multipart1974930991404280278.buf"}
not exist
FileData {fdInputName = "upfile", fdFileName = "MyWebApp_upload.hs", fdFileCType = "application/octet-stream", fdFilePath = "/var/folders/hw/p4bp49hd7v9_1j60sjvmhqnr0000gn/T/servant-multipart8933518161505795335.buf"}