I am trying to send a post
request to mongohq server from my AppEngine using rest api
.
I use this code:
public static void post(String id, String context)
{
String urlString = "https://api.mongohq.com/databases/db/collections/collection/documents?_apikey=XXXXXXXXXX";
String data = "{\"document\" : {\"_id\": \"" + id+ "\", \"context\":" + context +"}}";
try
{
URL url = new URL(urlString);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setInstanceFollowRedirects(false);
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/json");
connection.setRequestProperty("charset", "utf-8");
connection.setRequestProperty("Content-Length", "" + Integer.toString(data.getBytes().length));
connection.setUseCaches(false);
DataOutputStream wr = new DataOutputStream(connection.getOutputStream());
wr.writeBytes(data);
wr.flush();
wr.close();
System.out.println(connection.getResponseMessage());
connection.disconnect();
}
catch (Exception e)
{
System.out.println("postToMongo: "+ e);
}
}
It works perfectly from outside Appengine, but when I do the same from inside Appengine nothing is sent to mongodb.
The function prints OK
in both outside and inside Appengine, but the data appears in the db only when I use this function outside Appengine.
Also, a simple jave get
function works both outside and inside Appengine.
Can anyone help me with this problem? I search for a way to post to Mongohq from inside Appengine.
Thanks