Restful style usage of Google AJAX Language API

29 views
Skip to first unread message

Haisheng HU

unread,
Nov 30, 2009, 10:47:34 AM11/30/09
to Google AJAX APIs
Hello, Group,

I'm not talking about the AJAX function running at browser side. But
I'm interested in the restful style API from the following location at
server side.
http://ajax.googleapis.com/ajax/services/language/translate

So my questions are:
1) Does it support POST? You know, the GET method may have a length
limit of 2000 characters. That's too short for translating an essay.

2) I occasionally encountered these two different errors while feeding
different 'q' param (text to be translated). What's the cause of them?
Your client has issued a malformed or illegal request.
The requested URL /ajax/services/language/translate... is too large to
process.

Thanks in ad!


~haisheng

Jeremy Geerdes

unread,
Nov 30, 2009, 10:55:07 AM11/30/09
to google-ajax...@googlegroups.com
To answer your questions as you asked them:

1) Yes, the RESTful interface supports POST requests. Via POST, you
can submit source strings as long as 5,000 characters.

2) To fix the malformed request, make sure that your q string and
langpair are urlencoded, even if you're using POST. Especially check
to check that the vertical pipe ( | ) in the langpair. And regarding
the url that's too long, make sure that your url, with all parameters
correctly encoded, is coming in less than 2,000 characters. This
shouldn't be a problem if you use POST, as long as you stay within the
5K character limit.

Jeremy R. Geerdes
Effective website design & development
Des Moines, IA

For more information or a project quote:
http://jgeerdes.home.mchsi.com
http://jgeerdes.blogspot.com
http://jgeerdes.wordpress.com
jgee...@mchsi.com

Unless otherwise noted, any price quotes contained within this
communication are given in US dollars.

If you're in the Des Moines, IA, area, check out Debra Heights
Wesleyan Church!

And check out my blog, Adventures in Web Development, at http://jgeerdes.blogspot.com
!
> --
>
> You received this message because you are subscribed to the Google
> Groups "Google AJAX APIs" group.
> To post to this group, send email to google-ajax...@googlegroups.com
> .
> To unsubscribe from this group, send email to google-ajax-searc...@googlegroups.com
> .
> For more options, visit this group at http://groups.google.com/group/google-ajax-search-api?hl=en
> .
>
>

Haisheng HU

unread,
Dec 1, 2009, 11:41:04 AM12/1/09
to Google AJAX APIs
Hi, Jeremy,

Thank you so much for such a quick response!

Now my code runs well on GAE. I'd like to share some portion of it for
anyone else in need.
# make sure s is no more than 4,000 characters long before
urlencoded
def translate(self, s, lang):
params = {'q': s, 'v': '1.0', 'langpair': '|%s' % lang,
'format': 'html'}
# payload can be as long as 5,000 characters after urlencoded
response = urlfetch.fetch(url = _GOOGLE_TRANSLATE_BASE,
payload = urllib.urlencode(params),
method = urlfetch.POST,
headers = {'Content-Type': 'application/x-www-form-
urlencoded'})
data = simplejson.loads(response.content)
if data['responseStatus'] == 200:
return data['responseData']['translatedText']
else:
return '<h1>' + _ERROR_TITLE + '</h1><p class="e">' + str
(data['responseStatus']) + '</p>'


On Nov 30, 11:55 pm, Jeremy Geerdes <jrgeer...@gmail.com> wrote:
> To answer your questions as you asked them:
>
> 1) Yes, the RESTful interface supports POST requests. Via POST, you  
> can submit source strings as long as 5,000 characters.
>
> 2) To fix the malformed request, make sure that your q string and  
> langpair are urlencoded, even if you're using POST. Especially check  
> to check that the vertical pipe ( | ) in the langpair. And regarding  
> the url that's too long, make sure that your url, with all parameters  
> correctly encoded, is coming in less than 2,000 characters. This  
> shouldn't be a problem if you use POST, as long as you stay within the  
> 5K character limit.
>
> Jeremy R. Geerdes
> Effective website design & development
> Des Moines, IA
>
> For more information or a project quote:http://jgeerdes.home.mchsi.comhttp://jgeerdes.blogspot.comhttp://jgeerdes.wordpress.com
> jgeer...@mchsi.com
>
> Unless otherwise noted, any price quotes contained within this  
> communication are given in US dollars.
>
> If you're in the Des Moines, IA, area, check out Debra Heights  
> Wesleyan Church!
>
> And check out my blog, Adventures in Web Development, athttp://jgeerdes.blogspot.com
Reply all
Reply to author
Forward
0 new messages