How to parse a json output from a rest curl command as an attribute value in the default.rb file
Use Case:
In the Chef recipe Attributes --> attributes.rb file has the following default attributes which will be consumed by my recipe and these values needs to be randomly generated using a curl command which outputs it in form of a json output
default['agent']['id'] = 'F73D3CA4-!#!#-653A-XXXX-BBBBBB'
default['agent']['token'] = '90F1C7EA-*()*-YYYY-2528-ZZZZZZ'
This value is used in the recipes --> act.rb
Syntax used
tenant_id = node['agent']['id']
token = node['agent']['token']
The recipe makes use of the tenant_id and token in the following way in the recepie
dsa_args << " \"tenantID:#{tenant_id}\" \"tenantPassword:#{token}\""
The curl command to get the the id and token is:
curl --insecure -X GET -H "content-type: application/json" -H
"Accept: application/json" -d '{}'
https://XXX.XX.XXX.XX/rest/bat/rant/tenant?
name=Ante%20Data%20Wortyu%20Details
The Output is in the below format
{"id":"16","name":"Ante Data Wortyu Details","state":"ACTIVE", "tenantID":"F73D3CA4-!#!#-653A-XXXX-BBBBBB","tenantPassword":"90F1C7EA-()-YYYY-2528-ZZZZZZ"}
How would one pass the json formated output into my Chef Recipe or to the attributes file.
I went through the below link to get it completed but it fails with unauthorised errors:
https://www.twilio.com/blog/2015/10/4-ways-to-parse-a-json-api-with-ruby.html
Any help is greatly appreciated.
Thank you,
This is somewhat contrived and might not suit your entire use-case, but is an example of how you could parse the response body of an HTTP request as JSON and use it for Chef attributes.
# recipes/something.rb
require 'rest-client'
require 'json'
jsonResponse = JSON.parse(RestClient.get(URL))
node.override['agent']['id'] = jsonResponse["tenantID"]
node.override['agent']['token'] = jsonResponse["tenantPassword"]
Related
I'm using Confluent Avro Schema Registry with backward compatibility option. I upload this schema which is a record inside an array (the [ ]-brackets). This record only contains one string field.
curl -X POST -i -H "Content-Type: application/vnd.schemaregistry.v1+json" --data '{"schema": "[{\"type\": \"record\", \"name\": \"TestRecord\", \"namespace\": \"com.company\", \"fields\": [{\"name\": \"testname\", \"type\": \"string\"}]}]"}' http://localhost:8081/subjects/test99-value/versions
Everything goes well and I get a success response back (200).
Now I want to sanity check this so I remove the string field and I add a integer field. This should definitely not be compatible, neither forward nor backwards.
curl -X POST -i -H "Content-Type: application/vnd.schemaregistry.v1+json" --data '{"schema": "[{\"type\": \"record\", \"name\": \"TestRecord\", \"namespace\": \"com.company\", \"fields\": [{\"name\": \"testname2\", \"type\": \"int\"}]}]"}' http://localhost:8081/subjects/test99-value/versions
The problem is that this also returns a success even though this cannot possibly be correct. Am I missing something very basic here?
If I do this with a record that isn't inside an array I get the expected error from the schema registry.
I am trying to access Smartsheet through API 2.0 in a Ruby-on-Rails app. I can get he following curl command to work
curl --request GET https://api.smartsheet.om/2.0/sheets -H "Authorization: Bearer xxxxxxxxxxxxxxxxxxxxxxxxx"
All attempts to replicate in Ruby code have failed. Can anyone help convert to Ruby?
Thanks!
You have a handful of options to accomplish that. If you want to keep close to curl as you have proposed, you could capture the response of the curl command wraping it with backticks
response = `curl --request GET https://api.smartsheet.om/2.0/sheets -H "Authorization: Bearer xxxxxxxxxxxxxxxxxxxxxxxxx"`
I'd recommend, however, use something like CURB, which provides Ruby binding for libcurl. It would add some syntax sugar, so your request would look like
response = Curl.get("https://api.smartsheet.om/2.0/sheets") do |http|
http.headers['Authorization'] = 'Bearer xxxxxxxxxxxxxxxxxxxxxxxxx'
end
Yet another option would be ditching curl altogether and use a gem like HTTParty. It makes use of Ruby's net/http to make requests.
That would be something along these lines:
response = HTTParty.get("https://api.smartsheet.om/2.0/sheets",
headers: 'Authorization' => 'Bearer xxxxxxxxxxxxxxxxxxxxxxxxx')
I am using Ruby on Rails 4.1 and I am trying to implement an API with a custom mime type. That is, in config/initializers/mime_types.rb I register an alias as like the following:
Mime::Type.register_alias 'application/json', :my_json
From another system I am trying to access the API with curl by running a HTTP PUT request, this way:
curl http://www.my_api.org/articles.my_json --request PUT --header "Content-Type: application/json" --data-binary '{\"key\": {\"a\": \"1\", \"b\": \"2\"}}'
However, by inspecting the ArticlesController parameters in my Rails application, I get the following output (note: article parameters are "unwanted" and those duplicate the "wanted" key parameters):
Parameters: {"key": { "a"=>"1", "b"=>"2" }, "article": { "a"=>"1", "b"=>"2" }}
What is the problem? Is it a bug? How can I solve that?
Note: I have implemented and access other similar API by executing HTTP GET requests and all works as expected. The problem seems to happens only when I execute HTTP PUT requests.
#rafaelfranca - No it is not a bug. It is how wrap_parameters works. You can disable at this file in your application config/initializers/wrap_parameters.rb.
See github.
I have a simple question regarding to the usage of cURL. Didn't find much during my Google search or Man page to get a clear answer.
In here talks about using either --data vs --form on sending file/attachment. I'm curious to know what are the main difference and under what scenarios you would pick --data-binary VS --form ?
The POST "body" can be sent via either --data (for application/x-www-form-urlencoded) or --form (for multipart/form-data):
-F "foo=bar" # 'foo' value is 'bar'
-F "foo=<foovalue.txt" # the specified file is sent as plain text input
-F "foo=#foovalue.txt" # the specified file is sent as an attachment
-d "foo=bar"
-d "foo=<foovalue.txt"
-d "foo=#foovalue.txt"
-d "#entirebody.txt" # the specified file is used as the POST body
--data-binary "#binarybody.jpg"
The difference is explained in the HTML 4.01 Specification section on Forms:
application/x-www-form-urlencoded is the default content type.
The content type "application/x-www-form-urlencoded" is inefficient for sending large quantities of binary data or text containing non-ASCII characters. The content type "multipart/form-data" should be used for submitting forms that contain files, non-ASCII data, and binary data.
That's exactly the main difference, type of data that's being sent to the server (application/x-www-form-urlencoded vs multipart/form-data)
I am doing curl POST to a service with body containing some json data. I am getting the response as "request body was not mime-encoded as application/octet-stream". What does the response mean?
You probably need to supply a Content-Type header. Depending on what your web server is expecting, you might want to supply it either the mimetype "text/plain" or perhaps "application/x-www-form-urlencoded". In Curl, just include the argument:
-H "Content-Type: text/plain"
So your request will be something like:
curl -i -X POST --data-binary "#your.json" -H "Content-Type: text/plain"
Or substitute "text/plain" for the appropriate mime-type.
So, probably what's happening at the moment is that your web server is being given the content type application/octet-stream, and not understanding what to do with your plain text json content. If you run curl with "-v" it will give you a verbose description of the sent and received headers so you can see what Content-Type it's giving your server by default.
The input have to be a "application".
What you asking for is a mimetype.
Read more here: http://en.wikipedia.org/wiki/Internet_media_type
Please add more information for more informations.