向 Chef 发出 POST 请求

Making a POST request with Chef

我需要 post 将一个 .json 文件发送到服务器 API 和 Chef 食谱,按照 Chef's documentation 我想出了这个代码:

http_request '/tmp/bpp.json' do
  url 'http://localhost:8080/api/v1/blueprints/bpp'
  headers ({
    'AUTHORIZATION' => "Basic #{Base64.encode64(user)}",
    'CONTENT-TYPE' => 'aplication/json'
    })
  action :post
end

对于授权令牌,user是一个包含'user:password'

的变量

当我 运行 这个厨师食谱时,我得到以下响应:

Error executing action `post` on resource 'http_request[POST /tmp/bpp.json]'

Net::HTTPServerException
------------------------
400 "Bad Request"

在此之前,我只是执行了一个 curl 调用并且它工作正常,但我需要更改为 http_request 资源。这是旧的(有效的)curl 请求:

curl --user user:password -H 'X-Requested-By:My-cookbook' --data @/tmp/bpp.json localhost:8080/api/v1/blueprints/bpp

我不太习惯 REST api,对我来说似乎是一个未知领域。

你忘了 message。使用文件名作为资源名不会将此文件作为数据发送。尝试添加:

...
message lazy { IO.read('/tmp/bpp.json') }
...

在您的情况下,只会发送资源名称 - /tmp/bpp.json。不是文件内容。如链接文档中所述:

The message that is sent by the HTTP request. Default value: the name of the resource block See “Syntax” section above for more information.