简体   繁体   English

Rails接收HTTP POST请求

[英]Rails Receive HTTP POST Request

I have a Rails app which receives a POST request from a NGINX Module. 我有一个Rails应用,该应用从NGINX模块接收POST请求。 The request looks like as follows: 该请求如下所示:

Parameters: {"name"=>"asdadsasdads", "type"=>"live"}

I want to take the name and check in my Database if its the same, if yes I want to give a 200 back, if not 404. 我想取这个名字,并在我的数据库中检查它的名称是否相同,如果是,我想还给200,如果不是404。

def publish
  ssss = Ssss.find_by_name(params[:id])
  if ssss.name == params[:name]
    head 200, content_type: "text/html"
  else
    head 404, content_type: "text/html"
  end
end

This gives me a 500 Internal Server error. 这给了我一个500 Internal Server错误。

If I remove the "ssss.name" and make a ":name" (I was just testing around) it gives me a 404. And if I remove the "head 404, content_type: "text/html" from the else statement it gives me 200, but then every request will pass through, even if it's not the same as in my DB. 如果我删除“ sssss.name”并创建一个“:name”(我只是在测试),它会给我一个404。如果我从else语句中删除“ head 404,content_type:“ text / html”给我200,但每个请求都会通过,即使它与我的数据库中的请求不同。

I am not understanding how this even should work and I have a hard time googling. 我什至不知道这应该如何工作,而且我在使用Google时遇到了麻烦。 Can someone help me please? 有人能帮助我吗? What would be correct? 什么是正确的?

Thanks! 谢谢!

Update 1: 更新1:

def publish
  ssss = Ssss.find_by_name(params[:name])
  if ssss == params[:name]
    head 200, content_type: "text/html"
  else
    head 404, content_type: "text/html"
  end
end

That's what I am using right now. 那就是我现在正在使用的。 It gives me a 404 even though it should be fine?! 它可以给我一个404,即使应该没问题?!

"name"=>"55b5807bbde1", "type"=>"live"}
Ssss Load (0.1ms)  SELECT  "ssss".* FROM "ssss" WHERE "ssss"."name" = ? LIMIT 1  [["name", "55b5807bbde1"]]

If I change it to find(params[:id]), it gives me a 500 如果我将其更改为find(params [:id]),它会给我500

It would appear that your problem as of this writing is that you are storing an object of class Ssss in the variable ssss , then comparing this object to a String . 在撰写本文时,您似乎遇到的问题是,您正在将类Ssss的对象存储在变量ssss ,然后将该对象与String进行比较。 This will return false because the objects are not of the same class, throwing you into the 404 block. 这将返回false,因为对象不是同一类,从而使您陷入404块。

Try comparing Ssss.name to params[:name] and see what you get. 尝试将Ssss.nameparams[:name]进行比较,看看会得到什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM