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

Loïc Hoguin essen@REDACTED
Fri Apr 10 10:25:57 CEST 2015


No.

"provided" doesn't return a result back to the client, it returns the 
representation of the resource, which is only requested through the HEAD 
and GET methods.

GET has no request body so there's nothing to process.

Changing "accepted" into "provided" won't work because processing a 
request body and providing a response body are completely different 
operations.

I suggest reading the REST part of the guide here: 
http://ninenines.eu/docs/en/cowboy/1.0/guide/ - though if it confuses 
you then you should probably read the HTTP RFC 7231 or Fielding's REST 
chapter of his dissertation: 
http://www.ics.uci.edu/~fielding/pubs/dissertation/rest_arch_style.htm

If this is too difficult for you at this time I suggest using a plain 
HTTP handler.

On 04/10/2015 11:04 AM, 寂寂 wrote:
> So that's to say I have to use "accepted" to process the request body
> and then use "provided" to return a result back to client?  But If I use
> GET method I can both process request body and send back result in body
> in only "provided" function.
>
>
> ------------------ Original ------------------
> *From: * "Loïc Hoguin";<essen@REDACTED>;
> *Send time:* Friday, Apr 10, 2015 4:00 PM
> *To:* "寂寂"<44290016@REDACTED>;
> "erlang-questions"<erlang-questions@REDACTED>;
> *Subject: * Re: [erlang-questions] [erlang-question][cowboy] Why got 415
> unsupportedmediatype when using POST method
>
> "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";<essen@REDACTED>;
>  > *Send time:* Friday, Apr 10, 2015 3:28 PM
>  > *To:* "寂寂"<44290016@REDACTED>;
>  > "erlang-questions"<erlang-questions@REDACTED>;
>  > *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
>  >  > erlang-questions@REDACTED
>  >  > http://erlang.org/mailman/listinfo/erlang-questions
>  >  >
>  >
>  > --
>  > Loïc Hoguin
>  > http://ninenines.eu
>
> --
> Loïc Hoguin
> http://ninenines.eu

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



More information about the erlang-questions mailing list