3

I'm trying to remove a specific item from a FourSquare list, I was originally using the Foursquare2 gem but have been debugging with HTTParty. I'm doing the following request:

HTTParty.post("https://api.foursquare.com/v2/lists/#{list.id}/deleteitem?v=20120321&itemId=#{item.foursquare_id}&oauth_token=#{ENV['FOURSQUARE_TOKEN']}")

I'm sure that the list id and item id are correct, I double checked, but I get the following response:

param_error: Value is invalid for item id (400)
  • Are you supposed to use `item.id` or `item.foursquare_id` in your URL? – the Tin Man Jun 27 '13 at 16:03
  • @theTinMan i just put item.id to represent what i was doing, the value i'm actually using is the foursquare_id of that item. – FletchRichman Jun 27 '13 at 16:06
  • 1
    Ah. Don't do that. Be specific when explaining, because little inaccuracies like that are often the real problem in the code, or cause the question to have a long chain of comments as we try to sort out the real problem from the inaccurate description. – the Tin Man Jun 27 '13 at 16:07

1 Answers1

0

One thing I'd try is to build the URL programmatically using Ruby's built-in URI class:

require 'ostruct'
require 'uri'

#
# set up some variables just for example values
#
list = OpenStruct.new('id' => 'some_list_id')
item = OpenStruct.new('foursquare_id' => 'some_foursquare_id')
ENV['FOURSQUARE_TOKEN'] = 'foo'

uri = URI.parse("https://api.foursquare.com/v2/lists/#{ list.id }/deleteitem")
uri.query = URI.encode_www_form(
  'itemId' => item.foursquare_id,
  'oauth_token' => ENV['FOURSQUARE_TOKEN'],
  'v' => 20120321,
)

uri.to_s
=> "https://api.foursquare.com/v2/lists/some_list_id/deleteitem?itemId=some_foursquare_id&oauth_token=foo&v=20120321"

The reason this is a good idea, is that some values have to be encoded in a URL. Simply shoving them into a string won't do that for you. Instead, rely on a class that takes that stuff into account.

The Addressable::URI gem is even more comprehensive so look there if you need more features/power.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
  • Thanks, thats helpful to know. I tried my original code using venueId instead of itemId and it worked. I think the issue was that the itemId and venueId are the same, which wasn't making the foursquare endpoint very happy. I appreciate your help though. – FletchRichman Jun 27 '13 at 19:45