Skip to content

Instantly share code, notes, and snippets.

@etrepum
Created February 8, 2014 17:50
Show Gist options
  • Save etrepum/8887438 to your computer and use it in GitHub Desktop.
Save etrepum/8887438 to your computer and use it in GitHub Desktop.
$ curl -v -X PUT -H 'Content-Type:application-json' -H 'Transfer-Encoding:chunked' -d @empty_file 'http://127.0.0.1:8000/empty-file'
* About to connect() to 127.0.0.1 port 8000 (#0)
* Trying 127.0.0.1...
* Adding handle: conn: 0x7fada300d000
* Adding handle: send: 0
* Adding handle: recv: 0
* Curl_addHandleToPipeline: length: 1
* - Conn 0 (0x7fada300d000) send_pipe: 1, recv_pipe: 0
* Connected to 127.0.0.1 (127.0.0.1) port 8000 (#0)
> PUT /empty-file HTTP/1.1
> User-Agent: curl/7.30.0
> Host: 127.0.0.1:8000
> Accept: */*
> Content-Type:application-json
> Transfer-Encoding:chunked
>
* upload completely sent off: 5 out of 0 bytes
< HTTP/1.1 201 Created
* Server MochiWeb/1.0 (Any of you quaids got a smint?) is not blacklisted
< Server: MochiWeb/1.0 (Any of you quaids got a smint?)
< Date: Sat, 08 Feb 2014 17:47:34 GMT
< Content-Length: 29
<
Got a PUT request of 0 bytes
* Connection #0 to host 127.0.0.1 left intact
-module(put_request).
-export([ start/0, start_link/1, loop/1
]).
-define(LOOP, {?MODULE, loop}).
start() ->
spawn(
fun () ->
application:start(sasl),
start_link([{port, 8000}]),
receive
stop -> ok
end
end).
start_link(Options = [{port, Port}]) ->
io:format("Listening at http://127.0.0.1:~p/~n", [Port]),
mochiweb_http:start([{name, ?MODULE}, {loop, ?LOOP} | Options]).
loop(Req) ->
case Req:get(method) of
'PUT' ->
Len = Req:stream_body(8192, fun stream_body/2, 0),
Req:respond(
{201, [],
io_lib:format("Got a PUT request of ~p bytes\n", [Len])});
_Other ->
Req:respond({400, [], "Expecting a PUT request\n"})
end.
stream_body({BufLen, _Buf}, Acc) ->
Acc + BufLen.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment