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
This worked for me very well!
Below is the simple code to quickly check your rest api