[chef] Error while creating post request using REST API


Chronological Thread 
  • From: Sachin Gupta < >
  • To: " " < >
  • Cc: sachin kumar < >
  • Subject: [chef] Error while creating post request using REST API
  • Date: Fri, 17 Jul 2015 00:38:05 +0530

 Hi All,

 I am trying to create a post request using REST Client API in ruby.
  I have created a ruby class TargetService.rb, this class is having a method called createlockbox that will creates target. the class is described as

  #!/usr/bin/ruby

   require 'rubygems'
   require 'base64' 
   require 'json'

    class TargetService

       def initialize()   
           end
 
        def createLockbox ()
            puts "Inside create lockbox target function"
            #    user='admin'
            #    pwd='admin'
            #    url = ""https://devops.com:18102/opam"
   
 
 
    response = RestClient::Request.new(
        :method => :post,
        :url =">" 'https://devops.com:18102/opam/target',
        :targetType => 'lockbox',
        :user => 'admin',
        :password => 'admin',
        :targetName => 'Test-5',
        :domain => 'GDE',
        :host => 'Test-5',
        :content_type => 'application/json',
        :accept => 'application/json',
        ).execute
    results = JSON.parse(response.to_str)   
    p results 
    rescue RestClient::Exception => e
    puts e.http_body
    end
end

 I am calling this class from another ruby file as
 
 #!/usr/bin/env ruby

    require './TargetService'
       
    puts "Hello world!"   
    tar_ser = TargetService.new()
    target_create= tar_ser.createLockbox()   

   while executing this ruby file, I am getting error as    
  
   C:\Users\sachkkum\sachin\cookbooks\Test\recipes>ruby test1.rb
    Hello TargetService Class###########
    Hello world!
    Inside create lockbox target function
    A JSONObject text must begin with '{' at character 0 of
       
   

I had also tried this post request using browser based RESTClient (firefox). There I am able to create the target by passing these values in body and url as https://devops.com:18102/opam/target

{
"target":{
"targetType":"lockbox",
"targetName":"Test-5",
"host":"Test-5",
"domain":"GDE",
"description":"Create Test  target"
}
}

Getting Status code as "201 created" in response header.

Can anyone help, what wrong I am doing in this post request using ruby class?/

Thanks & Regards,
Sachin Gupta



Archive powered by MHonArc 2.6.16.

§