Re: Google Translate API 414 error (Request-URI Too Large)

654 views
Skip to first unread message

Andrew Beaven

unread,
Apr 6, 2013, 7:55:43 PM4/6/13
to google-ajax...@googlegroups.com
Sorry, wrong forum. Reposted in https://groups.google.com/forum/#!topic/google-translate-api/vFfr7lhWz1M

On Sunday, April 7, 2013 2:34:04 AM UTC+13, Andrew Beaven wrote:
Hi there,

I'm trying to call the Google Translate API using ajax like so:

var Utils = {
    translate: function(message, callback) {
        $.ajax({
            type: 'POST',
            url: 'https://www.googleapis.com/language/translate/v2',
            headers: { "X-HTTP-Method-Override": "GET" },
            dataType: 'jsonp',
            data: {
                key: '<my key here>',
                source: 'en',
                target: 'ru',
                q: message
            },
            success: callback
        });
    }
}

I'm finding that when the message is too large, I get a 414 error, indicating that the request URL is too large - it looks like the message is concatenated onto the request URL. I'm confused by this because I had thought that data sent in POST ajax requests were not sent via parameters in the URL. The translate docs mention if you want to sent lots of data, to do so via a POST, so I thought I'd be fine

Is there anything I'm doing wrong here?

Jeremy Geerdes

unread,
Apr 6, 2013, 10:16:30 PM4/6/13
to google-ajax...@googlegroups.com
That's okay. We used to support the Translate API over here and, in fact, are still happy to do so as we can. At any rate, the problem is that JSONP does not do POST requests. Instead of using a XMLHttpRequest, where you can control headers and request method, it uses a script tag, where you can only send information via GET. To do what you're wanting to do, you're going to need to build a proxy on your own domain/server using PHP, Perl, or some other server-side language. That way you can use an XMLHttpRequest to send the data via POST to your server, which will relay the request to Google's server and then return Google's response to your application.

Don't forget to tell them over in the Translate API forum that we provided the answer :D

jg

--
--
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
To view this message on the web, visit
https://groups.google.com/d/msg/google-ajax-search-api/-/XfBD35OP1m4J

For more options, visit this group at
http://groups.google.com/group/google-ajax-search-api?hl=en?hl=en
 
---
You received this message because you are subscribed to the Google Groups "Google AJAX APIs" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-ajax-searc...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 



--
Jeremy R. Geerdes
Generally Cool Guy
Des Moines, IA

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

Jeremy Geerdes

unread,
Apr 7, 2013, 6:53:20 AM4/7/13
to google-ajax...@googlegroups.com
To your first question: yes, that is exactly what jQuery is doing under the hood. And to your second question: that is a valid approach, but here are a couple of observations:

  1. To do this, you will actually need to keep your text at less than 2,000 characters. In fact, you may have to keep it substantially less than 2,000 characters. You see, at least older versions of MSIE had a URL length limit of 2,048 characters (or thereabouts. I didn't take the time to look up the exact number). Subsequently, Google has also established a length limit of about 2,000 characters on URLs to which their servers will respond (thus you get the error you were seeing). This means that the length of your *urlencoded* string can't be longer than 2,000 - the actual service URL - all other name/value pairs. By the time you get API key, callback, language parameters, etc., that can substantially lessen the length of the string you can pass, and even more so if you're using special characters. Each one of these will be translated into %xx format, potentially tripling the length of the string you actually send to the URL.
  2. Doing this inevitably impacts performance. For a string of 5,000 characters, instead of making one request to Google, you end up making at least three requests. This will slow your application.
  3. It also introduces a little more complexity into your application. If you're doing the requests asynchronously (best for performance), you're going to need be able to keep track of each translation as it comes back because they may not be returned in the same order you sent them. You'll need to use method closures, regardless of how you split the string.
  4. You'll also want to be very careful about how you split the string. The machine translation algorithms work better with long strings, particularly when they form complete clauses (e.g., sentences). So you would not want to just cut it off at the word break closest to the length you would need (much less the character at the word length you need!). Rather, you will want to break on commas or periods or equivalent grammatical marks (although, given the degradation of grammar which seems so prevalent today, you may not be able to count on such things).
  5. Regardless of how you proceed, you may be interested in the Javascript wrapper I built for the Translate API v2. You can find it here: https://code.google.com/p/gtranslate-api-v2-jsapi/ . It contains a couple of things which you may find useful. First, you can see the regex I use to split long strings on line 251 of jTranslate.js, as well as the way that I use it starting on line 216. And second, if you're wanting to avoid writing the server-side proxy because you aren't comfortable with server-side languages, the package includes just such an app written in PHP. The downside is that I just noticed that the server-side proxy is apparently having issues (don't know how I missed the emails about that issue), so I will have to address that in the next few days.
Hope these observations help!

jg



On Sun, Apr 7, 2013 at 3:54 AM, Andrew Beaven <ajbe...@gmail.com> wrote:
Awesome, thanks for your help mate. As you may have already guessed, I haven't used JSONP before and don't know much about it. In the google translate docs, there's an example under REST using JavaScript, is this what jquery does under the hood when you specify dataType: 'jsonp' in an ajax call?

Anyway, back to the question at hand... I'm thinking that because I will potentially need to translate more than 5K characters at a time, I think I'll go about this a different way, use a GET and if the text is bigger than 2K, I'll do multiple requests to the translate service and join the results back up when they return. Thoughts on this approach?

To view this message on the web, visit

For more options, visit this group at
http://groups.google.com/group/google-ajax-search-api?hl=en?hl=en
 
---
You received this message because you are subscribed to the Google Groups "Google AJAX APIs" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-ajax-search-api+unsub...@googlegroups.com.

For more options, visit https://groups.google.com/groups/opt_out.
 
 



--
Jeremy R. Geerdes
Generally Cool Guy
Des Moines, IA

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

--
--
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
To view this message on the web, visit

For more options, visit this group at
http://groups.google.com/group/google-ajax-search-api?hl=en?hl=en
 
---
You received this message because you are subscribed to the Google Groups "Google AJAX APIs" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-ajax-searc...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 
Message has been deleted

Adam Feldman

unread,
Apr 7, 2013, 7:03:29 PM4/7/13
to google-ajax...@googlegroups.com
Please find the Google APIs client library for .NET here:  https://code.google.com/p/google-api-dotnet-client/

It's currently in beta and under active development.  It is *not* deprecated.



On Sun, Apr 7, 2013 at 4:06 AM, Andrew Beaven <ajbe...@gmail.com> wrote:
Great observations - I have implemented already though didn't think about your 4th point so will need to handle that better. Though my site is a .net app, I'll definitely check out your library :D

Part of the reason I didn't look at server side solutions was ignorance - the first examples of the translate API usage I saw we're JS implementations so figured that was all that was available. When I realized that wasn't the case, I checked the docs for the .net library they have linked but it said something about it being deprecated.

Thanks for your help Jeremy, it's very much appreciated!

Andrew

For more options, visit this group at
http://groups.google.com/group/google-ajax-search-api?hl=en?hl=en
 
---
You received this message because you are subscribed to a topic in the Google Groups "Google AJAX APIs" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/google-ajax-search-api/9ZINRax3NRE/unsubscribe?hl=en.
To unsubscribe from this group and all its topics, send an email to google-ajax-searc...@googlegroups.com.

For more options, visit https://groups.google.com/groups/opt_out.
 
 

--
--
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
To view this message on the web, visit
Message has been deleted
Message has been deleted

Adam Feldman

unread,
Apr 8, 2013, 12:45:06 PM4/8/13
to google-ajax...@googlegroups.com
Thanks for pointing this out.  This is a mistake, and the "deprecation" notice on that sample will be removed.

Cheers,
Adam


On Sun, Apr 7, 2013 at 11:20 PM, Andrew Beaven <ajbe...@gmail.com> wrote:
Sorry, I was mistaken - it was the examples of use that say something about being deprecated (linked from here). It's probably just that method, though the docs should still be updated.
To unsubscribe from this group and all its topics, send an email to google-ajax-search-api+unsub...@googlegroups.com.

For more options, visit https://groups.google.com/groups/opt_out.
 
 

--
--
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

To view this message on the web, visit
http://groups.google.com/group/google-ajax-search-api?hl=en_US
For more options, visit this group at
http://groups.google.com/group/google-ajax-search-api?hl=en?hl=en
 
---
You received this message because you are subscribed to the Google Groups "Google AJAX APIs" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-ajax-search-api+unsub...@googlegroups.com.

For more options, visit https://groups.google.com/groups/opt_out.
 
 

--
--
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
To view this message on the web, visit
Reply all
Reply to author
Forward
0 new messages