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 <[email protected]> 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
> [email protected]
>
> 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
> !
>
> On Nov 30, 2009, at 9:47 AM, Haisheng HU wrote:
>
>
>
> > 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
>
> > --
>
> > You received this message because you are subscribed to the Google
> > Groups "Google AJAX APIs" group.
> > To post to this group, send email to [email protected]
> > .
> > To unsubscribe from this group, send email to
> > [email protected]
> > .
> > For more options, visit this group
> > athttp://groups.google.com/group/google-ajax-search-api?hl=en
> > .
--
You received this message because you are subscribed to the Google Groups
"Google AJAX APIs" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/google-ajax-search-api?hl=en.