khrt / Raisin

Raisin - a REST API micro framework for Perl 🐫 🐪
61 stars 29 forks source link

Disable the route extension. #99

Closed brunonymous closed 4 years ago

brunonymous commented 4 years ago

Hello,

Is it possible to disable the route extension (I use Raisin 0.82)?

The following function does not work: api_format 'json';

Due to the route extension, URLs path containing the dot character return the HTTP 406 error:

curl -v http://localhost:5000/api/v1/websites/foobar.com
*   Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to localhost (127.0.0.1) port 5000 (#0)
> GET /api/v1/websites/foobar.com HTTP/1.1
> Host: localhost:5000
> User-Agent: curl/7.58.0
> Accept: */*
> 
< HTTP/1.1 406 Not Acceptable-
< Transfer-Encoding: chunked
< Date: Tue, 11 Feb 2020 15:19:19 GMT
< Connection: keep-alive
< 
* Connection #0 to host localhost left intact

If the negotiate_format () method detects a point in the URL path, then this is automatically used as a format :

        if (_path_has_extension($req->path)) {
            $self->format_from_extension($req->path);
        }
        elsif (_accept_header_set($req->header('Accept'))) {
            # In case of wildcard matches, we default to first allowed format
            $self->format_from_header($req->header('Accept'), $allowed_formats[0]);
        }
        else {
            $self->default_format;
        }
    };

The code patched as follows works a little better:

        my $format;
        if (_path_has_extension($req->path)) {
            $format = $self->format_from_extension($req->path);
        }
        return $format if defined $format;
        if (_accept_header_set($req->header('Accept'))) {
            # In case of wildcard matches, we default to first allowed format
            $self->format_from_header($req->header('Accept'), $allowed_formats[0]);
        }
        else {
            $self->default_format;
        }
    };

But unfortunately the route param is "foobar" instead of "foobar.com".

What is the best practice for disabling the route extension?

Thank you in advance.

khrt commented 4 years ago

This is related to #14

brunonymous commented 4 years ago

Sorry, i hadn't seen this issue. So the only solution for now is to specify the .json extension at the end of the URL: curl -v http://localhost:5000/api/v1/websites/foobar.com.json

Until a better solution is found. :-) Thank you.