Ross Albertson
Ross Albertson

Reputation: 101

Dart won't let me use the POST method

I'm learning Dart, and I'm using the forcemvc package. My form renders, but when I click the button, I get the following error: The POST method is not allowed for /verify.

Here is my main function:

library mongomvc1;
// Copyright (c) 2015, <your name>. All rights reserved. Use of this source code
// is governed by a BSD-style license that can be found in the LICENSE file.

 import 'dart:html';
import 'package:forcemvc/force_mvc.dart';
part 'controllers/postcontroller.dart';
void main() {
//  querySelector('#output').text = 'Your Dart app is running.';
  WebApplication app = new WebApplication();
  app.host = "127.0.0.1";
  app.port = 8080;
  app.use("/", (req, model) => "index");
  app.use("/verify", (req, model) => "verify", method: "POST");
  app.start();
}

Here is my form:

 <form id="form" action="/verify" method="POST">
  <p>First name: <input type="text" id="first" name="first" required /></p>
  <p>Last name: <input type="text" id="last" name="last" required /></p>
  <p><button type="submit">Continue</button>
 </form> 

And here is my POST function:

part of mongomvc1;

@Controller
class PostController {

  @RequestMapping(value: "/verify", method: "POST")
  Future postMethod(req, Model model) async {
    req.GetPostParams().then((map) {
      model.addAttribute('first', map['first']);
      model.addAttribute('last', map['last']);
      req.Async(null);

    });
    model.addAttribute('status', 'ok');
    return req.AsyncFuture;
  }
}

What am I doing wrong?

Upvotes: 1

Views: 103

Answers (1)

Joris Hermans
Joris Hermans

Reputation: 191

I tested your code and you need todo the following:

library mongomvc1;
// Copyright (c) 2015, <your name>. All rights reserved. Use of this source code
// is governed by a BSD-style license that can be found in the LICENSE file.

import 'dart:async';
import 'dart:io';

import 'package:forcemvc/force_mvc.dart';
part 'controllers/postcontroller.dart';
void main() {
  WebApplication app = new WebApplication();
  app.host = "127.0.0.1";
  app.port = 8080;
  app.use("/", (req, model) => "index");
  // app.use("/verify", (req, model) => "verify", method: "POST");
  print("started");
  app.start();
}

/verify is already been used by the postcontroller.dart, so in this case you need to put it in comment or remove that line!

part of mongomvc1;

@Controller
class PostController {

  @RequestMapping(value: "/verify", method: "POST")
  Future postMethod(ForceRequest req, Model model) async {
    req.getPostParams().then((map) {
      model.addAttribute('first', map['first']);
      model.addAttribute('last', map['last']);
      req.async(null);
    });
    model.addAttribute('status', 'ok');
    return req.asyncFuture;
  }
}

All your methods on req need to be in lowercase instead of uppercase.

With the new async/await you can also write your code as follow:

part of mongomvc1;

@Controller
class PostController {

  @RequestMapping(value: "/verify", method: "POST")
  Future postMethod(req, Model model) async {
    var map = await req.getPostParams();
    model.addAttribute('first', map['first']);
    model.addAttribute('last', map['last']);

    model.addAttribute('status', 'ok');
  }
}

With these adjustments, the post should work. If you have any further questions, please ask!

Upvotes: 1

Related Questions