有没有办法允许Google App Engine通过DELETE请求发送正文或有效负载?

时间:2010-02-05 21:47:59

标签: http google-app-engine httpwebrequest

我正在尝试与需要将XML数据包含在HTTP DELETE请求正文中的API进行交互。我在AppEngine中使用urlfetch,而DELETE请求只是忽略了有效负载。

阅读本文后:Is an entity body allowed for an HTTP DELETE request?,我意识到标准可能不允许DELETE请求的正文内容,这就是urlfetch剥离正文的原因。

所以我的问题是:当urlfetch忽略有效负载时,是否有某种解决方法可以在app引擎中附加正文内容?

3 个答案:

答案 0 :(得分:6)

the docs

  

网址提取服务支持五种   HTTP方法:GET,POST,HEAD,PUT和   删除。请求可以包括HTTP   POST的标题和正文内容   或PUT请求。

鉴于GAE Python运行时是大量沙盒,所以你不太可能绕过这个限制。我认为这是一个错误,你应该提交错误报告here

答案 1 :(得分:2)

您可以通过套接字对body发出DELETE请求,检查HTTPRequest的示例Java代码,并对body执行不同的DELETE请求:

public static HTTPResponse execute(HTTPRequest request) throws ExecutionException, InterruptedException {

    if (request == null) {
        throw new IllegalArgumentException("Missing request!");
    }

    if (request.getMethod() == HTTPMethod.DELETE && request.getPayload() != null && request.getPayload().length > 0) {
        URL obj = request.getURL();
        SSLSocketFactory socketFactory = (SSLSocketFactory) SSLSocketFactory.getDefault();
        try {
            HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();

            HttpsURLConnection.setDefaultSSLSocketFactory(socketFactory);

            con.setRequestMethod("DELETE");
            for (HTTPHeader httpHeader : request.getHeaders()) {
                con.setRequestProperty(httpHeader.getName(), httpHeader.getValue());
            }
            con.setDoOutput(true);
            con.setDoInput(true);

            OutputStream out = con.getOutputStream();
            out.write(request.getPayload());
            out.flush();
            out.close();
            List<HTTPHeader> responseHeaders = new ArrayList<>();
            for (Map.Entry<String, List<String>> stringListEntry : con.getHeaderFields().entrySet()) {
                for (String value : stringListEntry.getValue()) {
                    responseHeaders.add(new HTTPHeader(stringListEntry.getKey(), value));
                }
            }
            return new HTTPResponse(con.getResponseCode(), StreamUtils.getBytes(con.getInputStream()), con.getURL(), responseHeaders);
        } catch (IOException e) {
            log.severe(e.getMessage());
        }
    } else {
        Future<HTTPResponse> future = URLFetchServiceFactory.getURLFetchService().fetchAsync(request);
        return future.get();
    }
    return null;
}

答案 2 :(得分:0)

您可以使用App Engine Socket API解决这个问题,以下是Go中的内容:

    client := http.Client{
        Transport: &http.Transport{
            Dial: func(network, addr string) (net.Conn, error) {
                return socket.Dial(c, network, addr)
            },
        },
    }