【问题标题】:Ruby Net::HTTP delete method with uri params and form data带有 uri 参数和表单数据的 Ruby Net::HTTP 删除方法
【发布时间】:2017-03-29 19:31:01
【问题描述】:

所以我可以像这样使用我的 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)

知道我在这里做错了什么吗?

【问题讨论】:

  • 服务器的响应是什么?服务器日志?
  • @diego.greyrobot 让我检查一下。我们的负载均衡器后面有很多 REST 服务器

标签: ruby http http-headers


【解决方案1】:

要在删除调用的 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=&gt; 'myaccount' :p=&gt;'someprofile'}

【讨论】:

    【解决方案2】:

    参数必须在正文中发送,正如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"
    

    【讨论】:

    • Ruby 出现以下错误:/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/net/protocol.rb:153:in `read_nonblock': 对等点重置连接 (Errno::ECONNRESET)
    • 这一行并不足以确定出了什么问题,但要冒险猜测一下,我会说身份验证。如果没有关于 API 如何处理身份验证的更多信息,我无法回答。 example types
    猜你喜欢
    • 1970-01-01
    • 2011-04-29
    • 1970-01-01
    • 2016-12-01
    • 2019-07-02
    • 2017-07-13
    • 2011-08-30
    • 2019-04-05
    • 1970-01-01
    相关资源
    最近更新 更多