簡體   English   中英

帶有uri參數和表單數據的Ruby Net :: HTTP刪除方法

[英]Ruby Net::HTTP delete method with uri params and form data

因此,我可以使用REST API成功獲得會話令牌,如下所示:

uri = URI.parse("#{@restapi}/users/login/cloud")
response = Net::HTTP.post_form(uri, {"username" => "jason", "password" => "password123"})

它返回以下JSON:

{ "username" : "jason" , "token" : "1234sometoken567890" , "account" : "myaccount" , "profile" : "main"}

我需要將令牌與表單數據一起使用,以調用API WADL中所述的此函數:

<resource path="/removeProfile">
  <method id="removeProfile" name="DELETE">
    <request>
      <representation mediaType="application/x-www-form-urlencoded">
        <param xmlns:xs="http://www.w3.org/2001/XMLSchema" name="a" style="query" type="xs:string"/>
        <param xmlns:xs="http://www.w3.org/2001/XMLSchema" name="p" style="query" type="xs:string"/>
      </representation>
    </request>
    <response>
      <representation mediaType="application/json"/>
    </response>
  </method>
</resource>

這告訴我在REST調用中需要執行以下操作:

1) Request Method:DELETE
2) Form Data a= and p=
3) append the token to the url

在瀏覽器中,它看起來像: https : //my.domain.com/rest/removeProfile?token=1234sometoken567890

與表單數據:

a=myaccount&p=someprofile

我在Ruby中嘗試過:

uri = URI.parse("#{@rest}/removeProfile")
# get the token with the connection code
uri.query = URI.encode_www_form(utk: "#{@token}")
http = Net::HTTP.new(uri.host, uri.port)
http.set_form_data({"a" => "#{@account}", "p" => "#{@profile}"})
request = Net::HTTP::Delete.new(uri.path)
response = http.request(request)

嘗試模仿curl這樣做(順便說一句不起作用):

curl -i -X DELETE "https://my.domain.com/rest/removeProfile?utk=1234sometoken567890&a=myaccount&p=someprofile"

用查詢字符串參數和表單數據發送http DELETE方法的正確方法是什么?

注意:

這是我當前的代碼:

  def remove_profile(account, profile)

    @account = account
    @profile = profile

    uri = URI.parse("#{@rest}/removeProfile")
    http = Net::HTTP.new(uri.host, uri.port)
    puts 'uri.path = ' + uri.path
    request = Net::HTTP::Delete.new(uri.path)
    request.body = "utk=#{@utk}&a=#{@account}&p=#{@profile}"
    puts 'request.body = ' + request.body
    response = http.request(request)

  end

Ruby炸毀了堆棧跟蹤,如下所示:

/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/net/protocol.rb:153:in `read_nonblock': Connection reset by peer (Errno::ECONNRESET)

知道我在這里做錯了嗎?

參數必須按照application/x-www-form-urlencoded在主體中發送。 在您的cURL示例中,您將必須添加-d標志

curl -X DELETE -d "a=myaccount&p=someprofile" "https://my.domain.com/rest/removeProfile"

req = Net::HTTP::Delete.new("/rest/removeProfile")
req.body = "a=myaccount&p=someprofile"

要在delete調用的url中傳遞參數,您可以執行以下操作:

    uri = URI.parse('http://localhost/test')
    http = Net::HTTP.new(uri.host, uri.port)
    attribute_url = '?'
    attribute_url << body.map{|k,v| "#{k}=#{v}"}.join('&')
    request = Net::HTTP::Delete.new(uri.request_uri+attribute_url)
    response = http.request(request)

其中body是您的參數的哈希圖:在您的示例中: {:a=> 'myaccount' :p=>'someprofile'}

暫無
暫無

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

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