I am using http_request resource in a chef recipe to make an http request which requires proxy user and password. I am facing problem substituting the variables defined in attributes or in fact any variables e.g. following code works fine, where username and password are hard coded.
http_request 'get-info' do
url "http://host:8080/v123/orgs/abc"
headers({ 'AUTHORIZATION' => "Basic #{ Base64.encode64('user1:pwd123')}",
'Content-Type' => 'application/json' } )
message ( "{ } " )
action :get
end
But if I use use variables instead of hard coded credentials like following
u_name=node['mychef']['username']
pwd=node['mychef']['password']
http_request 'get-info' do
url "http://host:8080/v123/orgs/abc"
headers({ 'AUTHORIZATION' => "Basic #{ Base64.encode64('#{u_name}:#{pwd}')}",
'Content-Type' => 'application/json' } )
message ( "{ } " )
action :get
end
then I get following error
================================================================================
Error executing action `get` on resource 'http_request[get-info]'
================================================================================
Net::HTTPServerException
------------------------
401 "Unauthorized"
Certainly the credentials are not getting read properly. Appreciate help on how to substitute variables in chef resource http_request.
I have resolved my question in the meantime. If I build the Base64 encoding separately in advance like following, then I can easily substitute the value in http_request
sys_admin_creds = Base64.encode64("#{node['mychef']['username']}:#{node['mychef']['password']}")
And then I can substitute value like following
headers({ 'AUTHORIZATION' => "Basic #{sys_admin_creds}",
'Content-Type' => 'application/json' } )
I think there might be other ways to solve this. When I directly substitute values in headers of http_request, I think the number of quotes and double quotes get unmanageable. But I am sure that can be fixed too. But for now I will go ahead with my solution. If anyone have better solution, still post it. So we can always improve our community fellows :).