I'm looking to print out the response from an API call with Kickbox's api.
This is what I have
require "kickbox"
client = Kickbox::Client.new('XXXXXXXXXXXXXXXXXXXXXXXXXXXX')
kickbox = client.kickbox()
response = kickbox.verify("test#example.com")
puts response
I'm not getting any response when trying to run the file in my terminal.
try response.body
{
"result" =>"unknown",
"reason" =>"no_connect",
"role" =>true,
"free" =>false,
"disposable" =>false,
"accept_all" =>false,
"did_you_mean" =>nil,
"sendex" =>0.35,
"email" =>"test#example.com",
"user" =>"test",
"domain" =>"example.com",
"success" =>true,
"message" =>nil
}
You can also get the response time and balance from headers from response.headers
{
"content-type" =>"application/json",
"x-kickbox-balance" =>"99",
"x-kickbox-response-time"=>"17"
}
Related
I was trying to send API request to my rails server from AWS Lambda function.
I was using httparty gem to send request.
I have tried with below code
require "httparty"
class PostManager
include HTTParty
def initialize
end
def create_post(job_id)
puts "----------------- Inside post manager ------------------"
puts "----------------- #{ENV["BASE_URI"]} ------------------"
puts "#{ENV['BASE_URI']}/call_response?job_id=#{job_id}"
response = HTTParty.get("#{ENV['BASE_URI']}")
puts "******************HTTP Response -: #{response}******************"
response
end
end
I am triggering this code from aws lambda main handler like below.
post_manager = PostManager.new
response = post_manager.create_post(job_id)
But lambda function gets timeout. Request not reaching to rails server at all.
Please guide me if i am missing something. Other alternatice to send post request to external server from aws lambda function is also invited.
Since http party is a http client, I recommend read the documentation, start experimenting with pry and a site like http://httpbin.org
So went will have all the thinks. reading your code I'm not sure of what you want to achieve, but I think that you want to connect to some en point that is on:
The domain inside this shell variable => #{ENV['BASE_URI']}
the path of the http method => call_response
and some path parameters like => job_id=#{job_id}
You allways say that this is a post but you are doing a get => HTTParty.get
So let's start with some object like the one showed in the documentation in order to attack to this method with curl will be something like this:
❯ curl -X GET "http://httpbin.org/get?job_id=4" -H "accept: application/json" ~/learn/ruby/stackoverflow
{
"args": {
"job_id": "4"
},
"headers": {
"Accept": "application/json",
"Host": "httpbin.org",
"User-Agent": "curl/7.64.1",
"X-Amzn-Trace-Id": "Root=1-613b73ef-2d00166a2ae40e704b448352"
},
"origin": "83.53.251.55",
"url": "http://httpbin.org/get?job_id=4"
}
Then for an http client object
add this to afile called httpbin_client.rb :
require 'httparty'
class HTTPbinClient
include HTTParty
base_uri ENV['BASE_URI']
def initialize
end
def ask_for_job_id(job_id)
self.class.get('/get', {query: {job_id: job_id}})
end
end
http_bin = HTTPbinClient.new
puts http_bin.ask_for_job_id(28)
call like this:
❯ BASE_URI=httpbin.org ruby httpbin_client.rb ~/learn/ruby/stackoverflow
{
"args": {
"job_id": "28"
},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip;q=1.0,deflate;q=0.6,identity;q=0.3",
"Host": "httpbin.org",
"User-Agent": "Ruby",
"X-Amzn-Trace-Id": "Root=1-613b776d-47036b9b29bb1ae34b4a0e50"
},
"origin": "83.53.251.55",
"url": "http://httpbin.org/get?job_id=28"
}
I am trying to send email using gmail api service account from spring boot api.
GoogleCredentials credentials = ServiceAccountCredentials.fromStream(inputStream)
.createScoped(Collections.singletonList(StorageScopes.DEVSTORAGE_FULL_CONTROL));
credentials = credentials.createScoped(SCOPES);
credentials.refresh();
AccessToken refreshToken = credentials.refreshAccessToken();
OAuth2Credentials credentialsAccess = OAuth2Credentials.create(refreshToken);
LOGGER.info("refresh token {}", credentials.refreshAccessToken().getTokenValue());
LOGGER.info("refresh token {}", credentialsAccess.getAccessToken().getTokenValue());
HttpRequestInitializer requestInitializer = new HttpCredentialsAdapter(credentials);
Gmail service = new Gmail.Builder(new NetHttpTransport(), JacksonFactory.getDefaultInstance(),
requestInitializer).setApplicationName(APPLICATION_NAME).build();
Users a = service.users();
sendMessage(service, "me", mimeMessage);
{
"code" : 400,
"errors" : [ {
"domain" : "global",
"message" : "Precondition check failed.",
"reason" : "failedPrecondition"
} ],
"message" : "Precondition check failed.",
"status" : "FAILED_PRECONDITION"
}
I'm trying to understand why I get 401.
Steps taken:
created a elasticsearch.keystore
add the bootstrap.password successfully, with cat ~/.elk.secret | /opt/elasticsearch/bin/elasticsearch-keystore add -x 'bootstrap.password'
Since I've multiple instances running, I move it in the new conf directory
but now I get 401:
curl -X GET 'https://elastic:myFancyPass#myServer:9200/_cluster/health?pretty' -k
{
"error" : {
"root_cause" : [
{
"type" : "security_exception",
"reason" : "unable to authenticate user [elastic] for REST request [/_cluster/health?pretty]",
"header" : {
"WWW-Authenticate" : [
"Bearer realm=\"security\"",
"ApiKey",
"Basic realm=\"security\" charset=\"UTF-8\""
]
}
}
],
"type" : "security_exception",
"reason" : "unable to authenticate user [elastic] for REST request [/_cluster/health?pretty]",
"header" : {
"WWW-Authenticate" : [
"Bearer realm=\"security\"",
"ApiKey",
"Basic realm=\"security\" charset=\"UTF-8\""
]
}
},
"status" : 401
}
My understanding is that this error can be caused by two things:
wrong password (which I doubt it's my case)
wrong elasticsearch.file
How do I find which elasticsearch.keystore it is loading?
Running rootLogger.level = debug didn't help. I feel it would be great getting a confirmation such as /opt/elasticsearch/instance2/conf/elasticsearch.keystore
I'm trying to use the Github API to create a file in a repo.
This CURL command does exactly what I want to do:
curl -X PUT -H 'Authorization: token <TOKEN>' -d '{"path": "test.txt", "message": "Test Commit", "committer": {"name": "Kevin Clark", "email": "kevin#kevinclark.ca"}, "content": "bXkgbmV3IGZpbGUgY29udGVudHM="}' https://api.github.com/repos/vernalkick/kevinclark/contents/test.txt
I need to do the same request but using rest_client in ruby, but this returns a 404:
require 'rest_client'
params = {
:path => "test.txt",
:message => "Test Commit",
:committer => {
:name => "Kevin Clark",
:email => "kevin#kevinclark.ca"
},
:content => "bXkgbmV3IGZpbGUgY29udGVudHM=",
:access_token => <TOKEN>
}
RestClient.put "https://api.github.com/repos/vernalkick/kevinclark/contents/test.txt", :params => params
Github's documentation: https://developer.github.com/v3/repos/contents/
So I finally found the solution to my problem!
I needed to create a json string instead of just passing the hash.
RestClient.put "https://api.github.com/repos/vernalkick/kevinclark/contents/test.txt", :params => JSON.generate(params)
I have a json object that I'm sending to Google's QXP Express API. The idea is that I send the object with the relevant travel information. In terminal, through curl, it's very easy to send it. I just use the following curl command. Doc.json is the file name of the json.
curl -d #doc.json --header "Content-Type: application/json" https://www.googleapis.com/qpxExpress/v1/trips/search?key=AIzaSyAaLHEBBLCI4aHLNu2jHiiAQGDbCunBQX0
This is my code to do it in Ruby.
uri = URI('https://www.googleapis.com/qpxExpress/v1/trips/search?key=MYAPIKEY')
req = Net::HTTP::Post.new uri.path
req.body = {
"request" => {
"passengers" => {
"adultCount" => 1
},
"slice" => [
{
"origin" => "BOS",
"destination" => "LAX",
"date" => "2014-10-14"
},
{
"origin" => "LAX",
"destination" => "BOS",
"date" => "2014-11-14"
}
]
}
}.to_json
res = Net::HTTP.start(uri.host, uri.port, :use_ssl => true) do |http|
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
http.ssl_version = :SSLv3
http.request req
end
puts res.body
However I'm getting back the following error.
{
"error": {
"errors": [
{
"domain": "global",
"reason": "parseError",
"message": "This API does not support parsing form-encoded input."
}
],
"code": 400,
"message": "This API does not support parsing form-encoded input."
}
}
I just need to send it with the json file, but nothing I can find online covers sending json's to APIs. Please help, I'm very stuck.
It is always matter of taste what tools you prefer, but as for me i am currently using the rest-client gem for accessing REST APIs. With this library your example could be written like this:
require 'json'
require 'rest-client'
response = RestClient.post 'https://www.googleapis.com/qpxExpress/v1/trips/search?key=AIzaSyAaLHEBBLCI4aHLNu2jHiiAQGDbCunBQX0',
{
request: {
passengers: {
adultCount: 1
},
slice: [
{
origin: "BOS",
destination: "LAX",
date: "2014-10-14"
},
{
origin: "LAX",
destination: "BOS",
date: "2014-11-14"
}
]
}
}.to_json,
:content_type => :json
puts response.body
But if you want a Net::HTTP only solution, this might not be a suitable answer for you.