POST json 到 rails 服务器 [英] POST json to rails server

查看:21
本文介绍了POST json 到 rails 服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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' 方法在控制器中并映射到/login",我正在设置正确的内容类型并接受来自 curl 客户端的标头.我一直收到 422 http 状态响应.

'create' method is in a controller and mapped to "/login", I am setting correct content types and accept headers from my curl client. I am getting a 422 http status response all the time.

有什么建议吗?

推荐答案

如果您发送的是正确的标头,那么您将不需要执行 "ActiveSupport::JSON.decode" -- rails 会为您完成.

If you are sending in the right headers, then you won't need to do "ActiveSupport::JSON.decode" -- rails will do that for you.

您需要在帖子中设置以下标题.

You'll need to set the following headers in your post.

Content-Type: application/json
Accept: application/json

A 422 表示不可处理实体——通常表示验证失败.

A 422 means Unprocessable Entity --- generally that there was a validation failure.

你应该可以拥有.如果不能,则说明您的标题设置不正确.

You should be able to have. If you can't, then your headers aren't set correctly.

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

这篇关于POST json 到 rails 服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆