[erlang-questions] [erlang-question][cowboy] Why got 415 unsupported media type when using POST method

Loïc Hoguin essen@REDACTED
Fri Apr 10 09:28:56 CEST 2015


Because you didn't define a content_types_accepted function and at least 
one function to accept the representation in the request.

On 04/10/2015 04:30 AM, 寂寂 wrote:
> Dear All,
>
> If I use GET method the REST API work properly, but when I change to
> POST, got 415 error
>
> My code:
>
> -export([init/2]).
> -export([allowed_methods/2, content_types_provided/2]).
> -export([to_json/2]).
> init(Req, Opts) ->
>      {cowboy_rest, Req, Opts}.
>
> allowed_methods(Req, State) ->
>      {[<<"POST">>], Req, State}.
>
> content_types_provided(Req, State) ->
>      {[
>        {<<"application/json">>, to_json}
>       ], Req, State}.
>
> to_json(Req, State) ->
>       .....
>
> when I test the REST API I:
> curl http://192.168.1.103:8080/cloud_init -v -XPOST -H"Content-Type:
> application/json" -d '{"client_id": "4342jltj3i"}'
>
> got :
> * Hostname was NOT found in DNS cache
> *   Trying 192.168.1.103...
> * Connected to 192.168.1.103 (192.168.1.103) port 8080 (#0)
>  > POST /cloudbox_init HTTP/1.1
>  > User-Agent: curl/7.37.1
>  > Host: 192.168.1.103:8080
>  > Accept: */*
>  > content-type: application/json
>  > Content-Length: 28
>  >
> * upload completely sent off: 28 out of 28 bytes
> < HTTP/1.1 415 Unsupported Media Type
> < connection: keep-alive
> * Server Cowboy is not blacklisted
> < server: Cowboy
> < date: Fri, 10 Apr 2015 00:56:15 GMT
> < content-length: 0
> < content-type: application/json
> <
> * Connection #0 to host 192.168.1.103 left intact
>
>
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions
>

-- 
Loïc Hoguin
http://ninenines.eu



More information about the erlang-questions mailing list