[Developers] Java and mql write service
Bryan Cheung
bryan.cheung at metaweb.com
Tue Feb 19 05:24:17 UTC 2008
Hi Christian,
I've had better luck using the Jakarta Commons HTTPClient library(http://hc.apache.org/httpclient-3.x/
).
In pseudo-code, you can login using the PostMethod:
private static final String LOGIN_PATH = "/api/account/login";
private static final String WRITE_PATH = "/api/service/mqlwrite";
private String freebaseUrl; // sandbox or www
PostMethod method = new PostMethod(freebaseUrl);
method.setPath(LOGIN_PATH);
method.setRequestBody(requestBody); // the request body
int responseCode = client.executeMethod(method); // execute method
The client object will store your cookie credentials and you can then
reuse the client to execute a write:
method.setPath(WRITE_PATH);
method.setRequestHeader("X-Metaweb-Request", "true");
method.setRequestHeader("Content-type", "application/x-www-form-
urlencoded");
method.setRequestBody(requestBody); // the request body
int responseCode = client.executeMethod(method);
Once upon a time, I wrote a FreebaseClient using HTTPClient and JSON
in Java (http://json.org/java/). If you're interested, I can send you
or anyone else some code samples.
Bryan
On Feb 18, 2008, at 7:07 PM, Christian Hirsch wrote:
> 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
> _______________________________________________
> Developers mailing list
> Developers at freebase.com
> http://lists.freebase.com/mailman/listinfo/developers
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.freebase.com/pipermail/developers/attachments/20080218/d669246f/attachment-0001.htm
More information about the Developers
mailing list