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

<div>&nbsp;</div>
<div>this is the code I&#39;m using:</div>
<div>&nbsp;<br>query = &quot;{\&quot;create\&quot;:\&quot;unless_exists\&quot;,\&quot;id\&quot;:null,\&quot;type\&quot;:\&quot;/common/topic\&quot;, \&quot;name\&quot;:\&quot;test_xy\&quot;}&quot;;<br>String envelope = &quot;{\&quot;qname\&quot;:{\&quot;query\&quot;:&quot; +query+ &quot;}}&quot;;<br>
String data = URLEncoder.encode(envelope, &quot;UTF-8&quot;);</div>
<div>&nbsp;</div>
<div>URL url = new URL(&quot;<a href="http://sandbox.freebase.com/api/service/mqlwrite" target="_blank">http://sandbox.freebase.com/api/service/mqlwrite</a>&quot;);<br>URLConnection conn = url.openConnection();<br>conn.setDoInput(true);</div>

<div>conn.setDoOutput(true);</div>
<div>&nbsp;</div>
<div>String credentials = &quot;metaweb-user=...&quot;; // value of the Set-Cookie from the login response&nbsp; <br>conn.setRequestProperty(&quot;Cookie&quot;, credentials);<br>conn.setRequestProperty(&quot;X-Metaweb-Request&quot;, &quot;True&quot;);<br>
conn.setRequestProperty(&quot;Content-type&quot;, &quot;application/x-www-form-urlencoded&quot;);</div>
<div>&nbsp;</div>
<div>OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());<br>wr.write(data);<br>wr.flush();</div>
<div>&nbsp;</div>
<div>BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));&nbsp;<br>//write the response to a JSON object...<br>wr.close();<br>rd.close();</div>
<div>&nbsp;</div>
<div>&nbsp;</div>
<div>Thanks,<br>Christian</div>