2010-02-05 16 views
7

我试图与一个需要XML数据被包含在HTTP DELETE请求正文中的API接口。我在AppEngine中使用urlfetch,并且DELETE请求仅仅忽略有效载荷。有没有办法允许Google App Engine通过DELETE请求发送主体或有效内容?

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

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

回答

6

the docs

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

鉴于GAE Python运行时严重受沙箱影响,您很有可能无法绕过此限制。我认为这是一个错误,你应该提交一个错误报告here

+1

同意,似乎是一个错误。 – 2010-02-05 23:37:07

+0

我同意,我已在此处对此问题进行了标记和评论:http://code.google.com/p/googleappengine/issues/detail?id=601&q=post%20delete&colspec=ID%20Type%20Status%20Priority%20Stars% 20Owner%20Summary%20Log%20Component – elkelk 2010-02-08 16:10:43

+0

elkelk,这个bug与这里的问题无关。 – 2010-02-09 14:11:45

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) 
      }, 
     }, 
    } 
2

您可以通过插座体,Java代码示例,来检查的HTTPRequest,并进行不同DELETE请求请求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; 
} 
相关问题