Update code Net::HTTP::Put.new(url.path)

HI ,

i am new to ROR

i am writing Ruby code for calling API blogs

i have written ruby code for

Creating the blog by

require 'net/http'
require 'uri'

url = URI.parse(‘http://localhost:3000/api/blogs/create.xml’)
req = Net::HTTP::Post.new(url.path)
req.basic_auth ‘a’, ‘a’
req.set_form_data({‘blogpost[title]’=>‘TestingAPIBlogposttitle’,
‘blogpost[description]’=>‘Testing api desc’,
‘blogpost[category_id]’=>‘3121’}, ‘;’)
res = Net::HTTP.new(url.host, url.port).start {|http| http.request(req)
}
case res
  when Net::HTTPSuccess, Net::HTTPRedirection
    puts res.body
  else
    res.error!
end

which runs successfully by creating a new blog

And i have a search code

require 'net/http'
require 'uri'
require 'cgi'

## Change this part according to the api to be accessed and the params
to be passed.
uri = URI.parse( "http://localhost:3000/api/blogs/show/blogtitle.xml" )

Aruna Chinnamuthu wrote:

HI ,

i am new to ROR

i am writing Ruby code for calling API blogs

i have written ruby code for

Creating the blog by

require 'net/http'
require 'uri'

url = URI.parse(‘http://localhost:3000/api/blogs/create.xml’)
req = Net::HTTP::Post.new(url.path)
req.basic_auth ‘a’, ‘a’
req.set_form_data({‘blogpost[title]’=>‘TestingAPIBlogposttitle’,
‘blogpost[description]’=>‘Testing api desc’,
‘blogpost[category_id]’=>‘3121’}, ‘;’)
res = Net::HTTP.new(url.host, url.port).start {|http| http.request(req)
}
case res
  when Net::HTTPSuccess, Net::HTTPRedirection
    puts res.body
  else
    res.error!
end

which runs successfully by creating a new blog

And i have a search code

require 'net/http'
require 'uri'
require 'cgi'

## Change this part according to the api to be accessed and the params
to be passed.
uri = URI.parse( "http://localhost:3000/api/blogs/show/blogtitle.xml" )

###
#blogtitle is the slug which is a mandatory , no other parameters

http = Net::HTTP.new(uri.host, uri.port)
request = Net::HTTP::Get.new(uri.path)
request.basic_auth 'a', 'a'
response = http.request(request)

puts response.body

which returns the

<?xml version="1.0" encoding="UTF-8"?>
<blogpost>
  <created-at type="datetime">2010-09-02T08:18:22Z</created-at>
  <description>&lt;p&gt;Blog desc&lt;/p&gt;</description>
  <slug>blogtitle</slug>
  <title>blogtitle</title>
  <user>
    <firstname>admin</firstname>
    <lastname>knome</lastname>
    <login>admin</login>
  </user>
</blogpost>

Now i am trying to Update a BLog

for this how to write the code

i tried by simply changing the POST.new by PUT.new

but it didnt works for me

I tried by adding

require 'net/http'
require 'uri'

url = URI.parse(‘http://localhost:3000/api/blogs/update/blogtitle.xml’)
req = Net::HTTP::Put.new(url.path)
req.basic_auth ‘admin’, ‘admin’
req.set_form_data({‘blogpost[title]’=>‘TestingAPIBlogposttitle’,
‘blogpost[description]’=>‘Testing api desc’,
‘blogpost[category_id]’=>‘379344121’}, ‘;’)
res = Net::HTTP.new(url.host, url.port).start {|http| http.request(req)
}
case res
  when Net::HTTPSuccess, Net::HTTPRedirection
    puts res.body
  else
    res.error!
end

but it shows me the 401 error even i gave the admin user credentials

maybe the api think you are trying yo insert with an existing id and no that you are updating?