Rails 4 Api 用户 Post 测试失败
Rails 4 Api Users Post test failing
我正在 Rails 4.2 API 中构建博客(用于 Angular 前端)。我正在添加具有设计的用户(我知道矫枉过正),并且我的控制器测试通过了所有内容。当我尝试 curl POST 请求或尝试从我的 Angular 应用程序创建用户时,我收到 406 Not Acceptable 状态代码。
这是我创建控制器的方法:
def create
user = User.new(user_params)
if user.save
render json: user, status: 201
else
render json: { errors: user.errors }, status: 422
end
end
private
def user_params
params.require(:user).permit(:email, :password, :password_confirmation)
end
我可以从 rails 控制台毫无问题地创建用户。
这是我一直在尝试的 curl 命令:
curl -i -H 'Accept: application/json' -H 'Content-type: application/json' -X POST -d '{"email":"person@mail.com","password":"12345678","password_confirmation":"12345678"}' http://api.myapi.dev/users
我也有这个资源:
api_users POST /users(.:format) api/users#create {:subdomain=>"api"}
我可以更新用户,并从 curl 执行任何其他操作而不会出现问题,我只是没有看到这里的问题。非常感谢任何帮助。
根据 W3,406
响应是:
The resource identified by the request is only capable of generating response entities which have content characteristics not acceptable according to the accept headers sent in the request.
所以当你使用cURL时,你不需要发送-H 'Accept: application/json' -H 'Content-type: application/json
,Rails会自动为你呈现JSON结果。因为你在控制器中只指定了format.json
。
Rails 希望您通过以下方式发送数据:
{ "users": { "email", "person@mail.com" }, ... }
当您使用 cURL 命令访问服务器时,您没有发送 users
。
我正在 Rails 4.2 API 中构建博客(用于 Angular 前端)。我正在添加具有设计的用户(我知道矫枉过正),并且我的控制器测试通过了所有内容。当我尝试 curl POST 请求或尝试从我的 Angular 应用程序创建用户时,我收到 406 Not Acceptable 状态代码。
这是我创建控制器的方法:
def create
user = User.new(user_params)
if user.save
render json: user, status: 201
else
render json: { errors: user.errors }, status: 422
end
end
private
def user_params
params.require(:user).permit(:email, :password, :password_confirmation)
end
我可以从 rails 控制台毫无问题地创建用户。
这是我一直在尝试的 curl 命令:
curl -i -H 'Accept: application/json' -H 'Content-type: application/json' -X POST -d '{"email":"person@mail.com","password":"12345678","password_confirmation":"12345678"}' http://api.myapi.dev/users
我也有这个资源:
api_users POST /users(.:format) api/users#create {:subdomain=>"api"}
我可以更新用户,并从 curl 执行任何其他操作而不会出现问题,我只是没有看到这里的问题。非常感谢任何帮助。
根据 W3,406
响应是:
The resource identified by the request is only capable of generating response entities which have content characteristics not acceptable according to the accept headers sent in the request.
所以当你使用cURL时,你不需要发送-H 'Accept: application/json' -H 'Content-type: application/json
,Rails会自动为你呈现JSON结果。因为你在控制器中只指定了format.json
。
Rails 希望您通过以下方式发送数据:
{ "users": { "email", "person@mail.com" }, ... }
当您使用 cURL 命令访问服务器时,您没有发送 users
。