<div>Hi All,</div>
<div> </div>
<div>I am trying to access the write API using Java, but somehow I always get an error.</div>
<div>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?</div>
<div> </div>
<div>this is the code I'm using:</div>
<div> <br>query = "{\"create\":\"unless_exists\",\"id\":null,\"type\":\"/common/topic\", \"name\":\"test_xy\"}";<br>String envelope = "{\"qname\":{\"query\":" +query+ "}}";<br>
String data = URLEncoder.encode(envelope, "UTF-8");</div>
<div> </div>
<div>URL url = new URL("<a href="http://sandbox.freebase.com/api/service/mqlwrite" target="_blank">http://sandbox.freebase.com/api/service/mqlwrite</a>");<br>URLConnection conn = url.openConnection();<br>conn.setDoInput(true);</div>
<div>conn.setDoOutput(true);</div>
<div> </div>
<div>String credentials = "metaweb-user=..."; // value of the Set-Cookie from the login response <br>conn.setRequestProperty("Cookie", credentials);<br>conn.setRequestProperty("X-Metaweb-Request", "True");<br>
conn.setRequestProperty("Content-type", "application/x-www-form-urlencoded");</div>
<div> </div>
<div>OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());<br>wr.write(data);<br>wr.flush();</div>
<div> </div>
<div>BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream())); <br>//write the response to a JSON object...<br>wr.close();<br>rd.close();</div>
<div> </div>
<div> </div>
<div>Thanks,<br>Christian</div>