Ruby on rails 具有用户身份验证的Ruby on Rails curl POST请求
我正在尝试对rails中的RESTAPI执行一个curl post请求。我尝试执行的卷曲请求是:Ruby on rails 具有用户身份验证的Ruby on Rails curl POST请求,ruby-on-rails,curl,Ruby On Rails,Curl,我正在尝试对rails中的RESTAPI执行一个curl post请求。我尝试执行的卷曲请求是: $ curl https://api.intercom.io/events \ -X POST \ -u pi3243fa:da39a3ee5e6b4b0d3255bfef95601890afd80709 \ -H "Content-Type: application/json" -d' { "event_name" : "invited-friend", "created_at": 1
$ curl https://api.intercom.io/events \
-X POST \
-u pi3243fa:da39a3ee5e6b4b0d3255bfef95601890afd80709 \
-H "Content-Type: application/json"
-d' { "event_name" : "invited-friend",
"created_at": 1391691571, "user_id" : "314159" }'
我已经在rails中看到了许多使用net/http进行API POST请求的示例,但是我从未找到一个在请求中添加-u的示例。如何执行此操作?我们可以使用basic\u auth
uri = URI('http://example.com/index.html?key=value')
req = Net::HTTP::Get.new(uri)
req.basic_auth 'user', 'pass'
res = Net::HTTP.start(uri.hostname, uri.port) {|http|
http.request(req)
}
puts res.body
我明白了。对于有类似问题的人:
data = {"event_name" => "invited-friend",
"created_at" => 1391691571, "user_id" => "314159" }
uri = URI("https://api.intercom.io/events")
header = {"Content-Type" => "application/json"}
https = Net::HTTP.new(uri.host,uri.port)
https.use_ssl = true
req = Net::HTTP::Post.new(uri.path, header)
req.basic_auth 'pi3243fa:da39a3ee5e6b4b0d3255bfef95601890afd80709', ''
req.body = data.to_json
res = https.request(req)
我需要做一个POST请求,如何将其与此代码结合起来?