I am trying to do a POST request using urlfetch under an app engine application.
I have followed the instructions (and code) extracted from the simple example found at the App Engine documentation (here https://developers.google.com/appengine/docs/java/urlfetch/usingjavanet), under the section "Using HttpURLConnection".
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.io.OutputStreamWriter;
String message = URLEncoder.encode("my message", "UTF-8");
try {
URL url = new URL("http://httpbin.org/post");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setRequestMethod("POST");
OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream());
writer.write("message=" + message);
writer.close();
if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
// OK
} else {
// Server returned HTTP error code.
}
} catch (MalformedURLException e) {
// ...
} catch (IOException e) {
// ...
}
In order to test this POST request, I am using the following website "http://httpbin.org/post".
The fetch and connection works - however, the connection is sent as a GET and not as POST.
Here is the response I get from for this request:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<title>405 Method Not Allowed</title>
<h1>Method Not Allowed</h1><p>The method GET is not allowed for the requested URL.</p>
Have anybody run into this issue ?
Any help is appreciated.