philwilson.org

Calling Yahoo REST web services with Ruby query parameter problems

29 November, 2006

I’m an idiot and req = Net::HTTP::Get.new(url.path+'?'+url.query) is my friend.

Just playing around with Ruby and the Yahoo Web Services. The example they give for calling a web service is this:

require 'net/http'

url = 'http://api.search.yahoo.com/WebSearchService/V1/webSearch?appid=YahooDemo&query=madonna&results=1'
resp = Net::HTTP.get_response(URI.parse(url)) # get_response takes an URI object

data = resp.body

I am behind a proxy at work, so my code looks like this:

require 'net/http'

@proxy_addr = 'wwwcache.bath.ac.uk'
@proxy_port = 3128

url = URI.parse('http://api.search.yahoo.com/WebSearchService/V1/webSearch?appid=YahooDemo&query=madonna&results=1')

req = Net::HTTP::Get.new(url.path)
res = Net::HTTP::Proxy(@proxy_addr, @proxy_port).start(url.host, url.port) {|http|
    http.request(req)
}

data = res.body

puts data

The response I get is:

<?xml version="1.0" encoding="UTF-8"?>
<Error xmlns="urn:yahoo:api">
  The following errors were detected:
  <Message>invalid value: appid (empty or missing)</Message>
  <Message>invalid value: query (empty or missing)</Message>
</Error>
<!-- ws01.search.re2.yahoo.com uncompressed Wed Nov 29 05:54:50 PST 2006 -->

So the query parameters are being stripped from the GET request. I have no idea why. Any ideas? I’ve not had this problem with making queries through our proxy, although a look with Wireshark could prove that they’re making it through.

Oops – I think I’m actually stripping the parameters myself there by creating a new Get using url.path but even changing to just do a puts http.request_get('WebSearchService/V1/webSearch?appid=YahooDemo&query=madonna&results=1').body doesn’t work, which I thought might have had a better chance.

See other posts tagged with general and all posts made in November 2006.