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

查看:79
本文介绍了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天全站免登陆