在 Chef Recipe 中将 cURL 请求转换为 http_request

Convert a cURL request to http_request in a Chef Recipe

有人可以帮助我如何将以下 cURL 请求转换为 http_request.

curl -X POST -u admin:admin -F backup=@'/tmp/backup2.xml' -v #{sonarqube_rest_uri}/qualityprofiles/restore 

我的 Vagrant box 的 /tmp 文件夹中有 backup2.xml。我可以通过编写 bash 脚本来发送请求,但我想要使用 http_request 资源的更简洁的方法。

你可以试试这个:

http_request 'Post backup xml' do
  headers ({
    'Content-Type' => 'multipart/form-data',
    'AUTHORIZATION' => "Basic #{Base64.encode64('admin:admin')}"
  })
  action :post
  url "#{sonarqube_rest_uri}/qualityprofiles/restore"
  message ::File.read("/tmp/backup2.xml")
end

-F flag indicates you are need the multipart/form-data header. You also need the authorization header for -u admin:admin, which the Chef docs have a good example的。 Action 和 url 非常简单。最后,将您的文件内容作为带有 ::File.read.

的消息读取

然而,关于 multipart/form-data 内容类型的 similar question 表明这种类型的内容可能不适用于 http_request。但是,希望这可以帮助您了解如何将 curl 请求转换为 http_request