[Developers] Java and mql write service

Christian Hirsch hirsch.christian at gmail.com
Wed Feb 20 20:28:28 UTC 2008


Hi,
thanks for the suggestions. The problem was in fact the missing "queries=".
As well as the urlencode of the envelope variable (which is not neccessary).

I am now using the Commons HTTPClient library like Bryan suggested and it
seems to work.
here is the code:

//login
myclient = new HttpClient();
PostMethod method = new PostMethod(BASE_URL);
method.setPath("/api/account/login");
method.setParameter("username", username);
method.setParameter("password", password);
int responseCode = myclient.executeMethod(method);
//write
query =
"{\"create\":\"unless_exists\",\"id\":null,\"type\":\"/common/topic\",
\"name\":\"testxy\"}";
String envelope = "queries={\"qname\":{\"query\":" +query+ "}}";
PostMethod method = new PostMethod(BASE_URL);
method.setPath("/api/service/mqlwrite");
method.setRequestHeader("X-Metaweb-Request", "true");
method.setRequestHeader("Content-type",
"application/x-www-form-urlencoded");
method.setRequestBody(envelope);
int responseCode = myclient.executeMethod(method);

Cheers,
Christian


On Feb 20, 2008 7:29 AM, Ben Drees <ben at metaweb.com> wrote:

> Hi Christian,
>
> The HTTP error response bodies usually provide clues as to what's going
> on.
>
> It looks like you're getting back response bodies that include the message
> "one of query=, or queries= must be provided", which points to a missing
> "queries="
> prefix in your 'envelope' variable.
>
> -Ben
>
> Dae Park wrote:
> > Do you have the message detail (JSON response) of the 400 response
> > you are getting?
> >
> > Make sure you are doing a POST and your post parameter looks like:
> >
> > queries={'qname':{'query':{...}}}
> >
> > -dae
> >
> > 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
> >>
> >
> > _______________________________________________
> > Developers mailing list
> > Developers at freebase.com
> > http://lists.freebase.com/mailman/listinfo/developers
> >
> >
>
> _______________________________________________
> 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/20080221/214b9db7/attachment-0001.htm 


More information about the Developers mailing list