def create
req = ActiveSupport::JSON.decode(request.body)
if user = User.authenticate(req["email"], req["password"])
session[:user_id] = user.id
render :json => "{\"r\": \"t\"}" + req
else
render :json => "{\"r\": \"f\"}"
end
end
'create' 方法在 Controller 中并映射到“/login”,我正在设置正确的内容类型并接受来自 curl 客户端的 header 。我一直收到 422 http 状态响应。
有什么建议吗?
最佳答案
如果您发送正确的 header ,则无需执行“ActiveSupport::JSON.decode”——rails 会为您完成。
您需要在帖子中设置以下标题。
Content-Type: application/json
Accept: application/json
A 422 表示无法处理的实体 --- 通常表示验证失败。
你应该能够拥有。如果不能,则说明您的 header 设置不正确。
def create
if user = User.authenticate(params["email"], params["password"])
session[:user_id] = user.id
render :json => "{\"r\": \"t\"}" + req
else
render :json => "{\"r\": \"f\"}"
end
end
关于ruby-on-rails - POST json 到 Rails 服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4914745/