Masahiro Okubo

Filter chain halted as :validate_sign_up_params rendered or redirected Completed 422 Unprocessable Entity in 4ms (Views: 1.4ms | ActiveRecord: 0.0ms | Allocations: 193)

Filter chain halted as :validate_sign_up_params rendered or redirected Completed 422 Unprocessable Entity in 4ms (Views: 1.4ms | ActiveRecord: 0.0ms | Allocations: 193)

flutterでrailsのdevise token authに新規登録にリクエストを投げたところ、タイトルのエラーが発生

pry-remoteで確認したところ中身がなかったので、flutterの方に問題がある可能性

[1] pry(#<Api::Auth::RegistrationsController>)> params
=> <ActionController::Parameters {"controller"=>"api/auth/registrations", "action"=>"create"} permitted: false>

flutterは

final url = 'http://10.0.2.2:3001/api/auth';
    try {
      final response = await http.post(url,
        body: json.encode(
          {
            'name': 'masahiro',
            'email': email,
            'password': password,
            'password_confirmation': password,
          },
        ),
      );
.....

headersがないので追加してみます

// flutterのコード
 final response = await http.post(url,
        headers: {'Content-Type': 'application/json'},
        body: json.encode(
          {
            'name': 'masahiro',
            'email': email,
            'password': password,
            'password_confirmation': password,
//            'returnSecureToken': true,
          },
        ),
      );

// railsのコンソール
[1] pry(#<Api::Auth::RegistrationsController>)> params
=> <ActionController::Parameters {"name"=>"masahiro", "email"=>"stmhamachiii@gmail.com", "password"=>"11111111", "password_confirmation"=>"11111111", "controller"=>"api/auth/registrations", "action"=>"create", "registration"=>{"name"=>"masahiro", "email"=>"stmhamachiii@gmail.com", "password"=>"11111111", "password_confirmation"=>"11111111"}} permitted: false>
[2] pry(#<Api::Auth::RegistrationsController>)> 

headersを追加したところ、リクエストが通りました!

目次
  1. 参考記事

参考記事

HTTP POST with Json on Body – Flutter/Dart


関連記事

copyright© 2016-2021 Masahiro Okubo