2

我一直在尝试处理渡槽中的帖子请求。阅读文档后,这就是我能够想出的 channel.dart

 router
.route("/chat")//"/chat/[:id]")
.link(() => ChatController());

聊天控制器.dart

> import 'package:web_api/web_api.dart';

    class ChatController extends ResourceController{

      @Operation.get('id')
      Future<Response> getProjectById(@Bind.path("id") int id) async {
        // GET /chat/:id
        print(id);
        //return Response.ok({"key": "value"});
      }

      @Operation.post()
      Future<Response> createChat(@Bind.body() Chat chat) async {
        // POST /project

        print("post");
        final Map<String, dynamic> body = await request.body.decode();
        final name =body['name'] as String;
        print(" 1) name ==> $name");

        //return Response.ok({"key": "value"});
      }

    }
    class Chat extends Serializable{
       int id;
      String name;

      @override
      void readFromMap(Map<String, dynamic> map) {
        id = map['id'] as int;
        name = map['name'] as String;
      }

      @override
      Map<String, dynamic> asMap() {
        return {
          'id': id,
          'name': name
        };
      }
}

最后是 html 模板

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <form action="http://127.0.0.1:8888/chat" method="POST">
        <input id="id" name="id">
        <input id="name" name="name">
        <button type="submit">Submit</button>
    </form>

</body>
</html>

aquaduct 不提供 html 模板。这是一个不同的位置。当我提交表单时,我的控制台日志。

[INFO] aqueduct: Server aqueduct/2 started.  
[INFO] aqueduct: POST /chat 15ms 415  

为什么我看不到正文内容,我怎么能看到正文(表单值)

4

2 回答 2

2

您收到 415 Media Type Unsupported 错误。您可以在日志中看到这一点,您也会在您的客户响应中看到它。

默认情况下, aResourceController只接受application/json数据。您必须acceptedContentTypes在控制器中设置以获取表单数据。最简单的方法是覆盖以下属性ChatController

class ChatController {
  ...
  @override
  List<ContentType> acceptedContentTypes = [ContentType("application", "x-www-form-urlencoded")];
  ...
}
于 2018-10-18T23:18:41.557 回答
1

对于所有具有相同问题的未来寻求者,应将上述问题附加到 Joe Conway 的答案中。在 html 模板更改中

  <form action="http://127.0.0.1:8888/chat" method="POST">

<form action="http://127.0.0.1:8888/chat" method="POST" enctype="application/x-www-form-urlencoded">

然后加

...
  @override
  List<ContentType> acceptedContentTypes = [ContentType("application", "x-www-form-urlencoded")];
  ...

到您的控制器,然后到您的可序列化类(在我的情况下为聊天类)。将字符串更改为列表。这些将如下

class Chat extends Serializable{
  List<String> id;
  List<String> name;

  @override
  void readFromMap(Map<String, dynamic> map) {
    id = map[0] as List<String>;
    name = map[1] as List<String>;
  }

  @override
  Map<String, dynamic> asMap() {
    return {
      'id': id[0],
      'name': name[1]
    };
  }
}

最后改变你在你的控制器中发布方法。

@Operation.post()
  Future<Response> createProject(@Bind.body() Chat chat) async {
    // POST /project

    print("post");
    Map<String, List<String>> body = await request.body.decode();
    final name =body["name"][0] ;

    print("\n\n 1) body ==> $body");
     print("\n\n 1) name ==> $name");


    return Response.ok({"key": "value"});
  }

我真诚地希望能帮助将来遇到同样问题的人。

于 2018-10-19T08:31:31.083 回答