Error while creating post request using REST API

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

On Thursday, July 16, 2015 at 12:08 PM, Sachin Gupta wrote:

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

The error is somewhere internal to the library you’re using. Looks like it’s trying to parse an empty string as JSON (or maybe there is leading whitespace) and the JSON library you’re using doesn’t like it. From the error it’s not possible to tell if the error is happening in the request or when trying to handle the response.

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

--
Daniel DeLeo

Yes Daniel you are correct, the json which I was initially passing was
containing some spaces and was not correct.

Now I had modified my json to
params = {
:target => {
:targetType => 'lockbox',
:targetName => 'Test-5',
:domain => 'GDE',
:host => 'Test-5',
:description => "Create Test Target"
}
}

$auth = 'Basic ' + Base64.encode64( "#{user}:#{pwd}" ).chomp
@resource = RestClient::Resource.new($url)
# p @resource
@response = @resource.post( params.to_json ,
:content_type =>
"application/json",
:accept => "application/json",
:Authorization => $auth)

Now I am able to create targets using REST Client api.

Thanks !!

Sachin

On Thu, Jul 23, 2015 at 11:58 PM, Daniel DeLeo dan@kallistec.com wrote:

On Thursday, July 16, 2015 at 12:08 PM, Sachin Gupta wrote:

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

The error is somewhere internal to the library you’re using. Looks like
it’s trying to parse an empty string as JSON (or maybe there is leading
whitespace) and the JSON library you’re using doesn’t like it. From the
error it’s not possible to tell if the error is happening in the request or
when trying to handle the response.

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

--
Daniel DeLeo