2012-01-29 15 views
3

投稿要求(JSON)を使用してユーザーを作成しようとしています。カールの例はうまくいきます。ここでcurlコマンドです:Rails/Devise - Unprocessable Entity

curl -X POST -H "Content-Type: application/json" 'http://localhost:3000/users.json' -d '{ "user": {"email": "[email protected]", "password": "foobar", "password_confirmation": "foobar"}}' 

カール出力:

Started POST "/users.json" for 127.0.0.1 at 2012-01-28 16:19:10 -0800 
    Processing by Devise::RegistrationsController#create as JSON 
    Parameters: {"user"=>{"email"=>"[email protected]", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}} 

が、私はJavaでHTTPライブラリとの要求をしようとすると、私はエラーを取得する:

Started POST "/users.json" for 192.168.1.88 at 2012-01-28 16:47:56 -0800 
    Processing by Devise::RegistrationsController#create as JSON 
    Parameters: {"user"=>"{\"password_confirmation\":\"secret\",\"password\":\"secret\",\"email\":\"[email protected]\"}"} 
Completed 422 Unprocessable Entity in 73ms (Views: 3.0ms | ActiveRecord: 0.0ms) 

コードリクエストを作成するために:

  RestClient client = new RestClient(SIGNUP_URL); 
      JSONObject jObj = new JSONObject(); 
      JSONObject jsonUserObj = new JSONObject(); 

      try { 
       jObj.put("email", txtUserName.getText().toString()); 
       jObj.put("password", txtPassword.getText().toString()); 
       jObj.put("password_confirmation", txtPassword.getText().toString()); 

       jsonUserObj.put("user", jObj.toString()); 
      } catch (JSONException e1) { 
       e1.printStackTrace(); 
      } 

      client.setJSONParams(jsonUserObj); 
      try { 
       client.Execute(RequestMethod.JSON_POST); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } catch (JSONException e) { 
        e.printStackTrace(); 
      } catch (Exception e) { 
        e.printStackTrace(); 
      } 
      if (client.getResponseCode() == HttpStatus.SC_OK) { 
       // Good response 
       try { 
        jObj = new JSONObject(client.getResponse()); 
        System.out.println("Signup Successful"); 
       } catch (JSONException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
      } 
     } 
ここでは210

実行のために呼び出される関数です:あなたは、ユーザーの内容はというというよりただ一つの大きなエスケープされた文字列であることがわかりJavaライブラリからのリクエストのログを見れば

HttpPost request = new HttpPost(url); 
      request.addHeader("Content-Type", "application/json"); 
      request.addHeader("Accept", "application/json"); 
      StringEntity s = new StringEntity(jsonParams.toString(), HTTP.UTF_8); 
      s.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json")); 
      request.setEntity(s); 
      executeRequest(request, url); 
      break; 
+0

ニーズに合わせて必要な変更を行うことができます。 – manmal

答えて

0

ネストされたハッシュあなたが要求を作成するためにあなたのコードを見てみると

あなたが持っている:

jsonUserObj.put("user", jObj.toString()); 

私はあなたにそれを変更した場合に推測している:

jsonUserObj.put("user", jObj); 

それが働くだろう、それ理由そのオブジェクトの文字列を "user"と関連付けるのではなく、jObjを "user"に関連付けるでしょう

0

同じ問題はの何らかのエラーからでした3210私は、問題は、文字列がエスケープされていることであることを確信している私はあなたと私のを共有すると、あなたは

class RegistrationsController < Devise::RegistrationsController 
    skip_before_filter :verify_authenticity_token, 
        :if => Proc.new { |c| c.request.format == 'application/json' } 

    respond_to :json 

    def create 
    build_resource 
    resource = User.new(params[:user]) 
    if resource.save 
     sign_in resource 
     render :status => 200, 
      :json => { :success => true, 
         :info => "Registered", 
         :data => { :user => resource, 
           :auth_token => current_user.authentication_token } } 
    else 
     logger.info("current user passed from json: #{resource.to_yaml}") 
     render :status => :unprocessable_entity, 
      :json => { :success => false, 
         :info => resource.errors, 
         :data => {} } 
    end 
    end 
end 
関連する問題