我试图使用ruby代码查询工件的aql api,我已经使用curl检查了这段代码在bash上是否有效:curl -u admin:password -i -H "Accept: application/json" -X POST http://server.example.com:8081/artifactory/api/search/aql -T aql.aql
其中aql.aql内容如下:
items.find
(
{
"repo":{"$eq":"test-ASO"}
}
)
.include("name","property.*")
现在我也尝试用ruby来实现这一点:
#!/usr/bin/ruby
require 'rubygems'
require 'json'
require 'open-uri'
require 'uri'
require 'net/http'
payload ='items.find
(
{
"repo":{"$eq":"test-ASO"}
}
).include("name","property.*")'
uri = URI.parse("http://server.example.com:8081/artifactory/api/search/aql")
http = Net::HTTP.new(uri.host,uri.port)
req = Net::HTTP::Post.new(uri.path)
req.basic_auth 'user', 'password'
req.set_form_data(payload)
res = http.request(req)
puts res.body
但我得到的是:
{
"errors" : [ {
"status" : 400,
"message" : "Bad Request"
} ]
}
我的猜测是,查询的有效负载必须是一个文件,就像我以前对curl(-t参数)所做的那样,但是我不认为使用文件进行查询是实现这一点的非常优雅的方法。
最佳答案
编辑
最后,我通过用适当的内容声明req.body变量实现了这一点:
#!/usr/bin/ruby
require 'rubygems'
require 'json'
require 'open-uri'
require 'uri'
require 'net/http'
payload = 'items.find().include("name","property.*")'
uri = URI.parse("http://server.example.com:8081/artifactory/api/search/aql")
http = Net::HTTP.new(uri.host,uri.port)
req = Net::HTTP::Post.new(uri.path)
req["Content-Type"] = "text/plain"
req.basic_auth 'admin', 'password'
req.body = payload
res = http.request(req)
puts res.body