Giter Site home page Giter Site logo

Comments (6)

essen avatar essen commented on May 24, 2024

If you don't provide content then doing a GET request will always fail, so I'm not sure what you are trying to say. What is the expected response?

Cowboy does handle the case where accept isn't defined when content is provided. https://github.com/ninenines/cowboy/blob/master/src/cowboy_rest.erl#L476-L486

Content negotiation is also useful for other methods. For example if you return a body following a POST request, it can tell you which content type to use.

The only cases where we don't care about content negotiation is when the resource never returns a body. In that case simply do not implement content_types_provided. Returning [] is for explicitly rejecting the request, for example if you apply additional heuristics on top of what Cowboy is doing.

from cowboy.

asabil avatar asabil commented on May 24, 2024

Imagine an endpoint where:

allowed_methods(Req, State) ->
    {[<<"OPTIONS">>, <<"HEAD">>, <<"GET">>, <<"POST">>, <<"DELETE">>], Req, State}.

and where only the GET method should send a content, whereas POST and DELETE should return 204, ideally we should be able to define content_types_provided as:

content_types_provided(#{method := <<"GET">>} = Req, State) ->
    {[
        {<<"application/json">>, to_json}
    ], Req, State};
content_types_provided(Req, State) ->
    {[], Req, State}.

However, this doesn't work, because the current cowboy_rest logic is tied to the fact that content_types_provided and doesn't handle the case where content_types_provided but returns the empty list.

from cowboy.

essen avatar essen commented on May 24, 2024

You don't need to go that far, you can just do:

content_types_provided(Req, State) ->
    {[
        {<<"application/json">>, to_json}
    ], Req, State}.

from cowboy.

asabil avatar asabil commented on May 24, 2024

I still think that implementing content_types_provided/2 and returning the [] (1) should be equivalent to not implementing it (2).

The current implementation handles (1) as reject all requests with 406, whereas (2) only returns 406 if the client actually sends an Accept header.

I suggest changing the implementation such as (1) and (2) are equivalent. Effectively handling the case where the client doesn't send any Accept header.

from cowboy.

essen avatar essen commented on May 24, 2024

They can't be equivalent because when the callback is not implemented it is equivalent to returning [{<<"text/html">>, to_html}].

If you need to have the equivalent you need to return this and not just [].

from cowboy.

essen avatar essen commented on May 24, 2024

https://github.com/ninenines/cowboy/blob/master/src/cowboy_rest.erl#L456

from cowboy.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.