You've got a lot of Gal

Yet another blog about Ruby, Rails, Javascript, Apple, Malteses and Shih-Tzus by Gal Steinitz

This site is completely static. It was last generated with Octopress and Jekyll on Sep 02, 2013 and hosted on github pages.

Google's Spell Check API Stopped Working - Here Is the Fix

Google’s undocumented, unpublished, not-for-public-consumption Spell Check API stopped working. How dare they? Well turns out the fix is simple, after using Charles to debug the HTTP requests of the Google Toolbar spell checker, I narrowed down the difference between my calls and theirs to a missing host header. Added that back in and everything works again. Usage: (I don’t parse the XML, because I’m just passing it to a flash client): [sourcecode language=”ruby”] => puts Spell.check(‘does it worka?’) <?xml version="1.0" encoding="UTF-8"?><spellresult error="0" clipped="0" charschecked="14"><c o="8" l="5" s="1">works work wok worker woke</c></spellresult> [/sourcecode] Source: [sourcecode language=”ruby”] require ‘rubygems’ require ‘builder’ require ‘net/http’ require ‘net/https’ module Spell HOST_HEADER = ‘www.google.com’ HOST_POST = ‘google.com’ PATH = "/tbproxy/spell?lang=en&hl=en&v=2.0f" def self.check(text) http = Net::HTTP.new(HOST_POST,443) http.use_ssl = true http.verify_mode = OpenSSL::SSL::VERIFY_NONE headers = {"Host" => HOST_HEADER} data = get_xml_payload(text) response_headers, response_data = http.post(PATH, data, headers) response_data end private def self.get_xml_payload(text) xml = Builder::XmlMarkup.new xml_string = xml.spellrequest(:textalreadyclipped => "0", :ignoredups => "0", :ignoredigits => "1", :ignoreallcaps => "0") do xml.text(text) end end end [/sourcecode]