Question

I'm trying to use Yahoo Content Analysis using a file containing text as input. So every character and length is possible. This code works with a simple text String (no special characters, short text) however when I use longer texts or special characters I get a Bad Request error (HTTP 400) sometimes with an error message like "no viable alternative at character '['" or without an error message. I encode every request and HTTP Post shouldn't have any limit as to the length. Does the Yahoo service place a limit on the length of the request and/or are there any characters that it can't handle? Any help to help this work is appreciated!

Here's my code (using commons-httpclient):

    String fileInput = FileUtils.readFileToString(f);

    StringBuilder builder = new StringBuilder();
    builder.append("http://query.yahooapis.com/v1/public/yql?");
    System.out.println(fileInput);
    builder.append("q=")
            .append(URLEncoder.encode("select * from contentanalysis.analyze where text='"+ fileInput +"'" , "UTF-8"))
            .append("&format=json");

    final String postUrl = builder.toString();
    HttpClient client = new HttpClient();
    PostMethod method = new PostMethod(postUrl);

    // Send POST request
    int statusCode = client.executeMethod(method);
Was it helpful?

Solution

I think the problem is that while you are sending the request as an HTTP POST, the YQL query and text are all included in the URL. YQL does not really have a way for you to make HTTP POST requests directly, so I can think of a couple options:

  1. Directly use the Content Analysis web service with an HTTP POST (docs)
  2. Create a custom YQL data table which uses the <execute> tag to run custom JavaScript which could do the POST (example)

Of these options I think the former would be easier.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top