question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

API routes getting 405: Method Not Allowed for API routes

See original GitHub issue

Bug report

Bug Description

405: Method Not Allowed, if trying POST using APIs Route

To Reproduce

mock.js files - this file under pages/apis

function Endpoint(req, res) {
    if(req.method === "POST"){
        res.json({ message: 'POST Hello Everyone!' });
    }else{
        res.json({ message: 'ELSE Hello Everyone!' });
    }
}

export default Endpoint;

Am getting response as below upon hitting http://localhost:3000/apis/mock :-


<!DOCTYPE html>
<html>
    <head>
        <link rel="shortcut icon" type="image/x-icon" href="/static/images/logo_transparent_favi.png"/>
        <meta name="viewport" content="width=device-width,minimum-scale=1,initial-scale=1" class="next-head"/>
        <meta charSet="utf-8" class="next-head"/>
        <title>405: Method Not Allowed</title>
        <link rel="preload" href="/_next/static/development/pages/_app.js?ts=1564029052989" as="script"/>
        <link rel="preload" href="/_next/static/runtime/webpack.js?ts=1564029052989" as="script"/>
        <link rel="preload" href="/_next/static/runtime/main.js?ts=1564029052989" as="script"/>
    </head>
    <body>
        <div id="__next">
            <div style="color:#000;background:#fff;font-family:-apple-system, BlinkMacSystemFont, Roboto, &quot;Segoe UI&quot;, &quot;Fira Sans&quot;, Avenir, &quot;Helvetica Neue&quot;, &quot;Lucida Grande&quot;, sans-serif;height:100vh;text-align:center;display:flex;flex-direction:column;align-items:center;justify-content:center">
                <div>
                    <style>body { margin: 0 }</style>
                    <h1 style="display:inline-block;border-right:1px solid rgba(0, 0, 0,.3);margin:0;margin-right:20px;padding:10px 23px 10px 0;font-size:24px;font-weight:500;vertical-align:top">405</h1>
                    <div style="display:inline-block;text-align:left;line-height:49px;height:49px;vertical-align:middle">
                        <h2 style="font-size:14px;font-weight:normal;line-height:inherit;margin:0;padding:0">Method Not Allowed
                            <!-- -->.
                        </h2>
                    </div>
                </div>
            </div>
        </div>
        <script src="/_next/static/development/dll/dll_92fa9139568d8c3d8f78.js?ts=1564029052989"></script>
        <script id="__NEXT_DATA__" type="application/json">{"dataManager":"[]","props":{"pageProps":{"statusCode":405}},"page":"/_error","query":{},"buildId":"development","dynamicBuildId":false}</script>
        <script async="" id="__NEXT_PAGE__/_app" src="/_next/static/development/pages/_app.js?ts=1564029052989"></script>
        <script src="/_next/static/runtime/webpack.js?ts=1564029052989" async=""></script>
        <script src="/_next/static/runtime/main.js?ts=1564029052989" async=""></script>
    </body>
</html>

Expected behavior

Should be able to resolve Http Methods??

System information

  • OS: Windows
  • Browser (if applies) chrome, also tried postman for POST
  • Version of Next.js: 9.0.2

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:12 (6 by maintainers)

github_iconTop GitHub Comments

4reactions
huv1kcommented, Jul 25, 2019

Hey @krnbr, your API routes should be under /api not /apis. POST methods are only allowed for API routes and not normal pages.

3reactions
pr4xxcommented, Jul 25, 2019

@huv1k How do I disable the method check? I have a custom server and now some parts of my app do not work anymore after updating to the latest version. I do not intend to use the new api directory (since I have a rather special use case).

Read more comments on GitHub >

github_iconTop Results From Across the Web

API Routes saying 405 Method not allowed - Laracasts
Error says that the GET Method is not supported but the method registered is a route method. I can't seem to figure out...
Read more >
Method 405 is not allowed, but in the API routing I defined POST
I am creating a SPA with the help of Laravel and Vue and the routing has worked previously, but for some reason it...
Read more >
405 - Method not Allowed from API requests
This article explains under what circumstances a 405 - "Method not allowed" error might occur. It includes information to help troubleshot the error....
Read more >
405 Method Not Allowed - HTTP - MDN Web Docs
The HyperText Transfer Protocol (HTTP) 405 Method Not Allowed response status code indicates that the server knows the request method, ...
Read more >
Problem with put requests? - Google Groups
version=v1, method=PUT, path=/api/:version/wines/:id(.:format)] But when I PUT to that route, I get a 405 Method Not Allowed: "NetworkError: 405 Method Not ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found