简体   繁体   English

Json API Curl到HTTParty请求。 该请求在我的本地机器上使用cURL,但在使用HTTParty的Heroku生产中不起作用

[英]Json API Curl to HTTParty request. The request works on my local machine with cURL, but not in Heroku production with HTTParty

So, I've gotten to the point that I'm not sure what question to ask anymore when researching my issue. 所以,我已经明白了,在研究我的问题时,我不确定要问什么问题。 So by coming here, I'm asking 2 things. 所以来到这里,我要问两件事。

First, how do I got about investigating issues like this in the future? 首先,我如何在将来调查此类问题? Second, what am I doing wrong right now? 第二,我现在做错了什么?

Basically, I created an app and pushed it to Heroku. 基本上,我创建了一个应用程序并将其推送到Heroku。 This app has API endpoints that work exactly as I expect them to when I am running the app locally and sending curl commands through my terminal. 当我在本地运行应用程序并通过终端发送curl命令时,此应用程序的API端点完全按照我的预期工作。 The code I'm running to try to sign-in to my app uses the HTTP gem and is returning {"status":500,"error":"Internal Server Error"} . 我正在尝试登录到我的应用程序的代码使用HTTP gem并返回{"status":500,"error":"Internal Server Error"}

So, this is my first time trying to connect to any API endpoint, so I'm flying a bit in the dark here. 所以,这是我第一次尝试连接到任何API端点,所以我在这里陷入困境。 What I read about a 500 error is, "When all else fails; generally, a 500 response is used when processing fails due to unanticipated circumstances on the server side, which causes the server to error out." 我读到的关于500错误的内容是,“当其他所有方法都失败时;通常,当处理因服务器端意外情况而失败时会使用500响应,这会导致服务器出错。” Which sounds a bit like a catch-all for somethings broken but we don't know what, other than it's on the backend. 这听起来有点像一个全能的东西,但我们不知道什么,除了它在后端。 Please correct me if I'm wrong. 如果我错了,请纠正我。

What do I do next? 接下来我该怎么办?

This is the curl command I'm running to send an email and password to receive and auth_token: 这是我正在运行的curl命令,用于发送电子邮件和密码以接收和auth_token:

curl -d "user_login[email]=fake@email.com&user_login[password]=password123" http://localhost:3000/api/v1/sign-in

As expected I get this back: 正如所料,我得到了回报:

{"auth_token":"628f3ebc47193665e7f1d32ae41ff9a7"}% 

This is the code I'm running using HTTParty to try and connect with my API in production using Heroku: 这是我使用HTTParty运行的代码,尝试使用Heroku在生产中连接我的API:

consume_api.rb consume_api.rb

require "rubygems"
require "httparty"

query_hash = { :user_login => "fake@email.com",
               :password => "password123" }

response = HTTParty.post("https://fake-heroku-94488.herokuapp.com/api/v1/sign-in", :query => query_hash)

puts response.body, response.code, response.message, response.headers.inspect

This is the response back: 这是回复:

{"status":500,"error":"Internal Server Error"}
500
Internal Server Error
{"server"=>["Cowboy"], "date"=>["Fri, 03 Feb 2017 14:49:40 GMT"], "connection"=>["close"], "content-type"=>["application/vnd.api+json; charset=utf-8"], "x-request-id"=>["5d094cee-11a2-4040-abfd-5180f5e46886"], "x-runtime"=>["0.003503"], "vary"=>["Origin"], "content-length"=>["46"], "via"=>["1.1 vegur"]}

To reiterate my questions are what can I take from this response to start investigating further? 重申我的问题是我可以从这个回答中得到什么来开始进一步调查? What questions should I be asking myself when this happens? 发生这种情况时,我应该问自己什么问题?

Second, What did I do wrong? 第二,我做错了什么?

Thanks in advance for your help. 在此先感谢您的帮助。

Edit: 编辑:

A quick update is that I ran the curl command that worked locally with the actual URL for my production site and it worked exactly as it does locally. 一个快速更新是我运行了curl命令,该命令在本地工作,与我的生产站点的实际URL一样,它的工作方式与本地工作完全相同。 I still can't manage to get further using HTTParty. 我仍然无法使用HTTParty进一步发展。

I checked out my heroku logs and found this: 我检查了我的heroku logs ,发现了这个:

2017-02-03T17:22:26.576272+00:00 heroku[router]: at=info method=POST path="/api/v1/sign-in" host=fake-heroku-94488.herokuapp.com request_id=9ab76ce0-95f3-4479-8672-874a624c070e fwd="108.11.195.58" dyno=web.1 connect=1ms service=8ms status=500 bytes=265
Started POST "/api/v1/sign-in" for 108.11.195.58 at 2017-02-03 17:22:26 +0000
Processing by Api::V1::SessionsController#create as JSON
  Parameters: {"email"=>"fake@email.com", "password"=>"[FILTERED]"}
Completed 500 Internal Server Error in 1ms (ActiveRecord: 0.0ms)

NoMethodError (undefined method `[]' for nil:NilClass):

app/controllers/api/v1/sessions_controller.rb:7:in `create'

Which points to line 7 of my SessionsController: 哪个指向我的SessionsController的第7行:

    ...
6          def create
7           resource = User.find_for_database_authentication(:email => params[:user_login][:email])
8            return invalid_login_attempt unless resource
    ...

Line 7 has two arrays in it, but I'm not sure how they're empty in the Httpary request and just find in the curl request. 第7行有两个数组,但我不确定它们在Httpary请求中是空的,只是在curl请求中找到它。

The answer to my first question of what I should do if I bump into this issue or one like it again is to check out the Heroku logs . 我的第一个问题的答案,如果我遇到这个问题或者再次讨论这个问题,我应该怎么做才能查看Heroku日志 That started the unwinding of my problems. 这开始解决我的问题。 The second part of this answer is that even when I figured out that I wasn't sending in the :user_login info, I still couldn't figure out how to fix my issue, so I started dropping byebug into anyplace related to my issue and asking what my variables values were and running code to see the output in console. 这个答案的第二部分是,即使我想通了,我不是在发送:user_login信息,我仍然无法弄清楚如何解决我的问题,所以我就开始下降byebug到任何地方与我的问题和询问我的变量值是什么,并运行代码以查看控制台中的输出。 When they kept coming back nil and I couldn't figure out what the right order/way to right the HTTParty request, I started putting .methods or .instance_methods after my variables to find more ways to ask what is going on here. 当他们不停地回来nil ,我无法弄清楚什么是正确的顺序/方式向右HTTParty要求,我开始把.methods.instance_methods我的变量后,找到更多的途径来问是怎么回事。 After that I spent time in rails console, using whatever instance_methods that were associated with my variables until I put the pieces together. 之后我花了一些时间在rails控制台中,使用与我的变量相关联的任何instance_methods,直到我把这些碎片放在一起。

The answer to my second question, what was I doing wrong? 我的第二个问题的答案是,我做错了什么? I wasn't sending in :user_login , mainly because I didn't quite understand what it was supposed to be doing (I'm new, I use a lot of tutorials that I understand like 95% of, this was from the 5%) and even when I did, I couldn't figure out how to shove it in my request. 我没有发送:user_login ,主要是因为我不太明白它应该做什么(我是新的,我使用了很多我理解的教程,如95%,这是来自5% )甚至当我这样做时,我无法弄清楚如何在我的要求中推它。 As it turns out, I also wasn't using :body appropriatly. 事实证明,我也没有使用:body适当。 Once I figure out that I need to put :body around all of the data that I was sending in, my next layer was :user_login then followed by my :email and :password , which with hindsight looks perfectly obvious now. 一旦我发现我需要放置:body围绕我发送的所有数据,我的下一层是:user_login然后是我的:email:password ,后见之明看起来非常明显。

This is what the code to sign in to my app looks like: 这是登录我的应用程序的代码如下所示:

consume_api.rb consume_api.rb

require "rubygems"
require "httparty"

response = HTTParty.post("https://fake-heroku-94488.herokuapp.com/api/v1/sign-in", :body => {:user_login => {:email => "fake@email.com", :password => "password123"}})


puts response.body, response.code, response.message, response.headers.inspect

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

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