Question

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.

Was it helpful?

Solution

As this question still gets a considerable amount of views 3 years later, I'll confirm here that the method given in the documentation sample works fine for making a POST request to the given URL, and has remained unchanged since this question was first posted. The working code sample is as follows:

String message = URLEncoder.encode("my message", "UTF-8");

URL url = new URL("http://httpbin.org/post");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setRequestMethod("POST");

OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream());
writer.write("message=" + message);
writer.close();

StringBuffer responseString = new StringBuffer();
String line;

BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
while ((line = reader.readLine()) != null) {
    responseString.append(line);
}
reader.close();

The following response was received:

Code: 200, message: { "args": {}, "data": "", "files": {}, "form": {
"message": "my message" }, "headers": { "Accept-Encoding": 
"gzip,deflate,br", "Content-Length": "18", "Content-Type": 
"application/x-www-form-urlencoded", "Host": "httpbin.org", "User-Agent": 
"AppEngine-Google; (+http://code.google.com/appengine; appid: s~ 
<redacted>)", "X-Cloud-Trace-Context": ",<redacted>" }, "json": null, 
"origin": "107.178.194.113", "url": "http://httpbin.org/post"}

OTHER TIPS

Have you tried calling the flush() method of the OutputStreamWriter?

Maybe you must set content-type request property:

connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top