Вопрос

How do I do an HTTP PUT request using the UrlFetch service for Google App Engine's Java runtime?

The following code in my application will send a POST request:

URL url = ...;
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(MAX_TIMEOUT); // Maximum allowable timeout on GAE of 60 seconds
conn.setRequestMethod("POST");
conn.setDoOutput(true);
conn.setRequestProperty("Content-Type", "application/json; charset=utf-8");
OutputStreamWriter writer = null;
try {
    writer = new OutputStreamWriter(conn.getOutputStream(), Charset.forName("utf-8"));
    writer.write(jsonContent);
} finally {
    if (writer != null)
        writer.close();
    }
}
InputStream inStream = null;
E response = null;   // Unmarshalled using Jackson
try {
    inStream = conn.getInputStream();
    if (status != 204) { // NO CONTENT => No need to use Jackson to deserialize!
        response = mapper.readValue(inStream, typeRef);
    }
} finally {
    if (inStream != null) {
        inStream.close();
    }
}

I try doing the same as POST for PUT, but I keep on getting a 405 HTTP error for Method Not Allowed. Thanks for the help.

Reference: https://developers.google.com/appengine/docs/java/urlfetch/overview

Unfortunately, GAE Java doesn't have as good of documentation as Python's version of GAE URL fetch(https://developers.google.com/appengine/docs/python/urlfetch/overview). Anyone from Google wanna say why?

Это было полезно?

Решение

I think your code works. The HTTP 405 you are seeing is coming from the server you're contacting (not all services support PUT). You can try using curl to hit the URL if you'd like confirmation that it's not your code at fault:

curl -X PUT -d "foo=bar" http://your_site.com/path/to/your/resource
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top