[Developers] Java and mql write service

Christian Hirsch hirsch.christian at gmail.com
Tue Feb 19 03:07:24 UTC 2008


Hi All,

I am trying to access the write API using Java, but somehow I always get an
error.
Sending read and login requests works just fine, however when I'm sending
write requests I'm always getting '400 bad request'. I'm not very familiar
with http requests in java... could there be something wrong with the
request header? or the credentials? - maybe someone has some working example
code?

this is the code I'm using:

query =
"{\"create\":\"unless_exists\",\"id\":null,\"type\":\"/common/topic\",
\"name\":\"test_xy\"}";
String envelope = "{\"qname\":{\"query\":" +query+ "}}";
String data = URLEncoder.encode(envelope, "UTF-8");

URL url = new URL("http://sandbox.freebase.com/api/service/mqlwrite");
URLConnection conn = url.openConnection();
conn.setDoInput(true);
conn.setDoOutput(true);

String credentials = "metaweb-user=..."; // value of the Set-Cookie from the
login response
conn.setRequestProperty("Cookie", credentials);
conn.setRequestProperty("X-Metaweb-Request", "True");
conn.setRequestProperty("Content-type",
"application/x-www-form-urlencoded");

OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write(data);
wr.flush();

BufferedReader rd = new BufferedReader(new InputStreamReader(
conn.getInputStream()));
//write the response to a JSON object...
wr.close();
rd.close();


Thanks,
Christian
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.freebase.com/pipermail/developers/attachments/20080219/c01c1916/attachment.htm 


More information about the Developers mailing list