Navarro Navarro - 15 days ago 4
reST (reStructuredText) Question

Making a POST request with Chef

I need to post a .json file to a server with a rest API with a Chef recipe, following Chef's documentation I came up with this code:

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


For authorization token,
user
is a variable that contains
'user:password'


When I run this chef recipe I obtain the following response:

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

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


Prior to this I was just executing a
curl
call and it was working fine, but I need to change to the
http_request
resource. This was the old (working)
curl
request:

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


I am not very used with REST apis and seems like an uncharted territory to me.

Answer

You forget about message. Using file name as resource name won't send this file as data. Try adding:

...
message your_json
...

In your case only the resource name - /tmp/bpp.json, will be sent. Not a file content. As stated in linked doc:

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