2

So I can successfully get a session token with my REST api like so:

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

It returns the following JSON:

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

I need to use the token with my form data to call this function as stated in the APIs 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>

This tells me that I need to do the following in my REST call:

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

In the browser it looks like: https://my.domain.com/rest/removeProfile?token=1234sometoken567890

with Form Data:

a=myaccount&p=someprofile

I tried this in 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)

Trying to mimic curl doing this (which doesn't work by the way):

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

What is the proper way to send a http DELETE method with Query String Parameters and form data?

NOTE:

Here is my current code:

  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 blows up with a stack trace like so:

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

Any idea what I am doing wrong here?

Jason Bain
  • 23
  • 4

2 Answers2

0

The Parameters have to be sent in the Body, as implied by application/x-www-form-urlencoded. In your cURL example you would have to add the -d flag

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"
jtzero
  • 2,204
  • 2
  • 25
  • 44
  • Ruby blow up with the following error: /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) – Jason Bain Jun 24 '15 at 19:53
  • That one line is not really enough to determine what's wrong but to venture a guess I would say authentication. Without more info on how the API handles authentication I can't answer it. [example types](http://stackoverflow.com/questions/319530/restful-authentication) – jtzero Jun 26 '15 at 03:23
0

To pass arguments in url of delete call, you can do the following:

    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)

where body is a hashmap of your parameters: in your example: {:a=> 'myaccount' :p=>'someprofile'}

user3683297
  • 156
  • 1
  • 8