4

I'm working on a virtual assistent using wit.ai in Java, but I'm stuck making the HTTP request. I'm no pro at HTTP requests in Java and I get a error 400 the whole time.

This is my code:

public class CommandHandler {
public static String getCommand(String command) throws Exception {

    String url = "https://api.wit.ai/message";
    String key = "TOKEN HERE";

    String param1 = "20141022";
    String param2 = command;
    String charset = "UTF-8";

    String query = String.format("v=%s&q=%s",
            URLEncoder.encode(param1, charset),
            URLEncoder.encode(param2, charset));


    URLConnection connection = new URL(url + "?" + query).openConnection();
    connection.setRequestProperty ("Authorization Bearer", key);
    connection.setRequestProperty("Accept-Charset", charset);
    InputStream response = connection.getInputStream();
    return response.toString();
}

}

This is the example wit.ai gives:

$ curl \
  -H 'Authorization: Bearer $TOKEN' \
  'https://api.wit.ai/message?v=20141022&q=hello'

I hope someone can help me out

Julian
  • 61
  • 4
  • I cannot say that it is the cause, but in curl you have `Authoritation: Bearer [value]` and in java `Authorization Bearer: [value]` (assuming both `TOKEN` and `key` are properly set). That said, you can print `url + "?" + query` to see if there is any further difference (although I would say there are not). – SJuan76 Feb 12 '16 at 09:13
  • 3
    Thanks, I guess I forgot the space after Bearer so it's `connection.setRequestProperty ("Authorization", "Bearer " + key);` – Julian Feb 12 '16 at 09:21
  • Hi @Julian, If you are aware of any open source project which is based in JAVA and is using wit.ai api's, could you please share? Thanks, I was checking on wit.ai project, they have node sdk, but not java sdk directly. Thanks. – a3.14_Infinity Nov 15 '16 at 06:27

2 Answers2

1

Below is the simple code to quickly check your rest api

try {
        URL url = new URL("https://api.wit.ai/message?v=20170218&q=Hello");
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("GET");
        conn.setRequestProperty("Accept", "application/json");
        conn.setRequestProperty("Authorization", "Bearer addkeyhere");

        if (conn.getResponseCode() != 200) {
            throw new RuntimeException("Failed : HTTP error code : "
                    + conn.getResponseCode());
        }

        BufferedReader br = new BufferedReader(new InputStreamReader(
            (conn.getInputStream())));

        String output;
        System.out.println("Output from Server .... \n");
        while ((output = br.readLine()) != null) {
            System.out.println(output);
        }

        conn.disconnect();

      } catch (MalformedURLException e) {

        e.printStackTrace();

      } catch (IOException e) {

        e.printStackTrace();

      }
Rahul Sawant
  • 1,223
  • 9
  • 12
0

This worked for me very well!

connection.setRequestProperty ("Authorization": "Bearer "+key);
ImGroot
  • 796
  • 1
  • 6
  • 17