2010-02-05 68 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; 
} 
相關問題