开发者

Net::HTTP::Put.new(url.path) shows the 401 error in RUby

开发者 https://www.devze.com 2023-01-15 16:27 出处:网络
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

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'
req开发者_如何学Pythonuire '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" )

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>k</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

its showing me the error even if i gave admin User credentials


It might be worth trying a POST request but also adding a _method = 'put' parameter to the request. Rails can simulate a PUT request in this way though I would expect it to respond correctly to an HTTP PUT too.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号