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

**URL:** <https://www.ruby-forum.com/t/update-code-net-put-new-url-path/194572>\
**Category:** Rails\
**Created:** [September 7, 2010, 10:51am UTC](https://www.ruby-forum.com/t/update-code-net-put-new-url-path/194572 "2010-09-07T10:51:06Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![arunachandu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d6d6ee/32.png) [@arunachandu](https://www.ruby-forum.com/u/arunachandu)\
**Post date:** [September 7, 2010, 10:51am UTC](https://www.ruby-forum.com/t/update-code-net-put-new-url-path/194572/1 "2010-09-07T10:51:06Z")

</div>

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](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](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"?\> 2010-09-02T08:18:22Z \<p\>Blog desc\</p\> blogtitle blogtitle admin knome admin 

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

---

<div class="post-metadata">

**Author:** ![arunachandu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d6d6ee/32.png) [@arunachandu](https://www.ruby-forum.com/u/arunachandu)\
**Post date:** [September 7, 2010, 11:09am UTC](https://www.ruby-forum.com/t/update-code-net-put-new-url-path/194572/2 "2010-09-07T11:09:36Z")

</div>

Aruna C. 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](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](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"?\> 2010-09-02T08:18:22Z \<p\>Blog desc\</p\> blogtitle blogtitle admin knome admin 
> 
> 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](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

---

<div class="post-metadata">

**Author:** ![arunachandu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d6d6ee/32.png) [@arunachandu](https://www.ruby-forum.com/u/arunachandu)\
**Post date:** [September 7, 2010, 4:03pm UTC](https://www.ruby-forum.com/t/update-code-net-put-new-url-path/194572/3 "2010-09-07T16:03:16Z")

</div>

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