用ruby从AWS Lambda发送API get请求



我试图从AWS Lambda函数向我的rails服务器发送API请求。

我正在使用httparty gem发送请求。

我试过下面的代码

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

我从aws lambda主处理程序触发此代码,如下所示。

post_manager = PostManager.new
response     = post_manager.create_post(job_id)

但是lambda函数超时了。请求没有到达rails服务器。

请指导我,如果我错过了什么。我们还邀请您使用其他方法从aws lambda函数向外部服务器发送post请求。

由于http方是http客户端,我建议阅读文档,开始试验窥探和一个网站,如http://httpbin.org

所以去会有所有的想法。阅读你的代码,我不确定你想要实现什么,但我认为你想要连接到某个点上:

shell变量内的域=># {ENV("BASE_URI")}HTTP方法的路径=>call_response还有一些路径参数,比如=>job_id = # {job_id}

你总是说这是一个帖子,但你在做get =>HTTParty.get

那么让我们从文档中展示的某个对象开始为了用curl攻击这个方法就像这样

❯ 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"
}

对于http客户端对象

将此添加到名为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)

像这样调用:

❯ 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"
}

最新更新