Closed tasinttttttt closed 8 years ago
works fine with POST.. what are you doing exactly? can you send me some steps to reproduce?
closing until i get some feedback
@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.
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
Hey there, Whenever I try a POST request, I get the "Method Not Allowed" message. Have I missed something in the configuration? or is local-web-server not intended to work with POST (as SimpleHTTPServer on python?).
Apart from that, thumbs up!