GithubHelp home page GithubHelp logo

POST method not allowed about local-web-server HOT 4 CLOSED

lwsjs avatar lwsjs commented on September 27, 2024
POST method not allowed

from local-web-server.

Comments (4)

75lb avatar 75lb commented on September 27, 2024 1

Probably the most simple way to create a custom response is to create a quick middleware plugin..

Try this plugin:

class Example {
  middleware (config) {
    return async (ctx, next) => {
      if (ctx.request.path === '/login' && ctx.request.method === 'POST') {
        const { username, password } = ctx.request.body
        if (password === '123') {
          ctx.response.body = `Welcome ${username}`
        } else {
          ctx.response.body = `Wrong password`
        }
      }
      await next()
    }
  }
}

export default Example

Save it to example.js then launch it like this

$ ws --stack lws-log lws-body-parser tmp/example.js -f dev

Make some test requests..

$ curl http://127.0.0.1:8000/login -d '{ "username": "Lloyd" }' -H 'content-type: application/json'
Wrong password

$ curl http://127.0.0.1:8000/login -d '{ "username": "Lloyd", "password": "123" }' -H 'content-type: application/json'
Welcome Lloyd

from local-web-server.

75lb avatar 75lb commented on September 27, 2024

works fine with POST.. what are you doing exactly? can you send me some steps to reproduce?

from local-web-server.

75lb avatar 75lb commented on September 27, 2024

closing until i get some feedback

from local-web-server.

Dok11 avatar Dok11 commented on September 27, 2024

@75lb I have the same issue while making a POST request like this "/api/v0/sendData.json". I want to have the mock to this request. If I use GET request, all fine.
And as I can see (https://github.com/lwsjs/local-web-server/wiki/How-to-create-a-mock-response), the mocks were deprecated. How can I use POST requests now, if possible?

P.S. I found this article https://github.com/lwsjs/local-web-server/wiki/How-to-prototype-a-REST-API
but not found an easier way to simply provide any files for POST requests.

from local-web-server.

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.