[英]Accessing a 3rd party API JSON object in ruby
Been messing around with Kickbox's api for email verification.一直在使用 Kickbox 的 api 进行电子邮件验证。 I'm trying to have the program only display the result object in the returned JSON.我试图让程序只在返回的 JSON 中显示结果对象。
Here's the code:这是代码:
require "kickbox"
require 'httparty'
require 'json'
client = Kickbox::Client.new('ac748asdfwef2fbf0e8177786233a6906cd3dcaa')
kickbox = client.kickbox()
response = kickbox.verify("test@easdfwf.com")
file = File.read(response)
json = JSON.parse(file)
json['result']
I'm getting an error verify.rb:10:in read': no implicit conversion of Kickbox::HttpClient::Response into String (TypeError) from verify.rb:10:in
'我收到一个错误 verify.rb:10:in read': no implicit conversion of Kickbox::HttpClient::Response into String (TypeError) from verify.rb:10:in
'
Here's a sample response:这是一个示例响应:
{
"result":"undeliverable",
"reason":"rejected_email",
"role":false,
"free":false,
"disposable":false,
"accept_all":false,
"did_you_mean":"bill.lumbergh@gmail.com",
"sendex":0,
"email":"bill.lumbergh@gamil.com",
"user":"bill.lumbergh",
"domain":"gamil.com",
"success":true,
"message":null
}
You are getting this error:您收到此错误:
read': no implicit conversion of Kickbox::HttpClient::Response into String (TypeError)
Because, in this line:因为,在这一行中:
file = File.read(response)
Your response
is a Kickbox::HttpClient::Response
type object, but the File.read
is expecting a String
object instead (possibly a file name with path).您的response
是Kickbox::HttpClient::Response
类型的对象,但File.read
期望的是String
对象(可能是带路径的文件名)。
I'm not sure what you are trying to do, but this: file = File.read(response)
is wrong.我不确定您要做什么,但是: file = File.read(response)
是错误的。 You can't do this and that's why you are getting the mentioned error.您不能这样做,这就是您收到上述错误的原因。
If you really want to use file, then you can write the response
to a file and then read the response
back from the file and use that:如果你真的想使用文件,那么你可以将response
写入文件,然后从文件中读取response
并使用它:
f = File.new('response.txt', 'w+') # creating a file in read/write mode
f.write(response) # writing the response into that file
file_content = File.read('response.txt') # reading the response back from the file
So, the issue is not about Accessing a 3rd party API JSON object in ruby , but you are trying to use File.read
in a wrong way.因此,问题不在于在 ruby 中访问第三方 API JSON 对象,而是您试图以错误的方式使用File.read
。
You can get the response
from the API by doing this:您可以通过执行以下操作从 API 获得response
:
client = Kickbox::Client.new('YOUR_API_KEY')
kickbox = client.kickbox()
response = kickbox.verify("test@easdfwf.com")
Then, you can play with the response
eg can do a puts response.inspect
or puts response.body.inspect
and see what's inside that object.然后,您可以使用response
例如可以执行puts response.inspect
或puts response.body.inspect
并查看该对象内部的内容。 And, from there you can extract your required outputs only.而且,您只能从那里提取所需的输出。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.