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 Atul
• 5,470 points
63 views

1 answer to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
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,520 points

Related Questions In Chef

+13 votes
2 answers
0 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,030 points
31 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
2,073 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,130 points
841 views
0 votes
1 answer

Support for ES6 modules syntax on IBM bluemix

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

answered Aug 9, 2018 in DevOps on Cloud by ajs3033
• 7,000 points
44 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
• 56,120 points
39 views
0 votes
1 answer

Python request module for bitcoin json rpc

This should work: #!/usr/bin/env python import getpass import json import requests ...READ MORE

answered Aug 28, 2018 in Blockchain by digger
• 27,620 points
134 views

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.