簡體   English   中英

Net :: HTTP :: Put.new(url.path)在RUby中顯示401錯誤

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

我是ROR的新手

我正在編寫用於調用API博客的Ruby代碼

我已經寫了紅寶石代碼

通過創建博客

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

通過創建一個新博客成功運行

而且我有一個搜索代碼

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

這將返回

<?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>

現在我正在嘗試更新博客

為此,如何編寫代碼

我嘗試通過簡單地將PUT.new更改為POST.new

但這對我沒有用

即使我提供了管理員用戶憑據,它也會顯示錯誤

嘗試POST請求可能值得,但也可以在請求中添加_method = 'put'參數。 Rails可以以這種方式模擬PUT請求,盡管我希望它也能正確響應HTTP PUT。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM