proper syntax for http request put request

0 votes

I am trying to us http_request in my chef cookbook:

http_request 'some request' do
  url 'https://'"#{node[:chef][:node_name]}"'/v1/xyz'
  headers ({
     'AUTHORIZATION' => "Basic #{Base64.encode64('#{node[\'user\']}:#{node[\'password\']')}",
    })
  action :get
end

But this come sup when I run it:

[2017-05-12T21:39:04-04:00] ERROR: http_request[some request] (mi_activemq::default line 58) had an error: OpenSSL::SSL::SSLError: SSL Error connecting to https://... - SSL_connect returned=1 errno=0 state=error: certificate verify failed

But If I hardcode the username:password it works:

'AUTHORIZATION' => "Basic #{Base64.encode64('username:password')}",
Jun 19, 2018 in Chef by ffdfd
• 5,550 points
1,236 views

1 answer to this question.

0 votes

You'll hvae to interpolate values into strings.

Start with this hash definition:

node = {
  chef: {
    node_name: 'foo'
  }
}

string definition:

'https://'"#{node[:chef][:node_name]}"'/v1/xyz'
# => "https://foo/v1/xyz"

Roby concatenates adjacent strings into one:

'foo'"bar"'baz' # => "foobarbaz"

But this way code becomes messy and is less human readable:

'foobarbaz' # => "foobarbaz"

similarly:

"foobarbaz" # => "foobarbaz"

Instead of the string use this:

"https://#{node[:chef][:node_name]}/v1/xyz"
# => "https://foo/v1/xyz"

Another problem with your code is :

require 'base64'
"Basic #{Base64.encode64('#{node[\'user\']}:#{node[\'password\']')}"
# > "Basic I3tub2RlWyd1c2VyJ119OiN7bm9kZVsncGFzc3dvcmQnXQ==\n"

the root of your problem:

'#{node[\'user\']}:#{node[\'password\']'
# => "\#{node['user']}:\#{node['password']"

basically when you execute the code, the value passe dto base64.encode64 is the string. You're varaible are never interpolated so you get:

# > "Basic I3tub2RlWyd1c2VyJ119OiN7bm9kZVsncGFzc3dvcmQnXQ==\n"

It doesn't have the correct user or password encoded in it.

Also check, that your node hash may not have user or password keys. A cleaner way to call encode64 is:

encoded_user_password = Base64.encode64(
  node.values_at('user', 'password').join(':')
)

Now interpolate into a string:

"Basic #{encoded_user_password}"

Also check ruby's URI class as it's the recommended way of building and manipulating URI's

answered Jun 19, 2018 by DareDev
• 6,890 points

Related Questions In Chef

+15 votes
2 answers

Git management technique when there are multiple customers and need multiple customization?

Consider this - In 'extended' Git-Flow, (Git-Multi-Flow, ...READ MORE

answered Mar 27, 2018 in DevOps & Agile by DragonLord999
• 8,450 points
4,034 views
+2 votes
1 answer
0 votes
1 answer

dBase for my website always gives me this error “Invalid syntax”

Seems like you are missing a close ...READ MORE

answered Mar 30, 2018 in Selenium by nsv999
• 5,500 points
1,194 views
0 votes
1 answer

"No mapping found for HTTP request with URI[....] in DispatherServlet"

Your standard Spring MVC application will serve ...READ MORE

answered Jun 6, 2018 in Java by prasad
• 160 points
21,686 views
0 votes
1 answer

Getting the proper xpath for date picker in selenium

Try to use explicit wait. Sometimes it ...READ MORE

answered Jul 5, 2018 in Selenium by Samarpit
• 5,910 points
4,343 views
0 votes
1 answer

Support for ES6 modules syntax on IBM bluemix

There is no native support for es6 ...READ MORE

answered Aug 10, 2018 in DevOps on Cloud by ajs3033
• 7,300 points
736 views
0 votes
1 answer

'Syntax Error: invalid syntax' for no apparent reason

You're missing a close paren in this ...READ MORE

answered Aug 13, 2018 in Python by Priyaj
• 58,020 points
2,447 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP