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

Loïc Hoguin <>
Fri Apr 10 10:00:06 CEST 2015


"accepted" is for processing the resource representation in the request 
body (POST/PATCH/PUT).

"provided" is for sending the resource representation in the response 
body (GET/HEAD, though HEAD will ultimately not send it).

On 04/10/2015 10:44 AM, 寂寂 wrote:
> Thats' to say I should use content_types_accepted rather than
> content_types_provided?
>
> I"m still puzzled with the difference between these two functions. when
> to use which?
>
> Why is it ok when I use GET method with content_types_provided?
>
> thanks!
>
>
> ------------------ Original ------------------
> *From: * "Loïc Hoguin";<>;
> *Send time:* Friday, Apr 10, 2015 3:28 PM
> *To:* "寂寂"<>;
> "erlang-questions"<>;
> *Subject: * Re: [erlang-questions] [erlang-question][cowboy] Why got 415
> unsupportedmedia type when using POST method
>
> 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
>  > 
>  > http://erlang.org/mailman/listinfo/erlang-questions
>  >
>
> --
> Loïc Hoguin
> http://ninenines.eu

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


More information about the erlang-questions mailing list