簡體   English   中英

使用Google Cloud Endpoints發送HTTP 204響應

[英]Send HTTP 204 response with Google Cloud Endpoints

我正在使用Google Cloud Endpoints創建API,如果沒有任何內容可以返回,我希望返回“無內容”HTTP 204響應。 我嘗試返回null,它在開發服務器上拋出一個錯誤,並在生產時發出非空結果,狀態代碼為200。

可以發出真正的204空響應或其他類型或自定義響應?

要為生產Python Cloud Endpoints API返回204 No Content ,您可以使用VoidMessage

from google.appengine.ext import endpoints
from protorpc import messages
from protorpc import message_types
from protorpc import remote

class MyMessage(messages.Message):
  ...

@endpoints.api('someapi', 'v1', 'Description')
class MyApi(remote.Service):

  @endpoints.method(MyMessage, message_types.VoidMessage,
                    ...)
  def my_method(self, request):
    ...
    return message_types.VoidMessage()

這當前在開發服務器上返回200,感謝您發現此錯誤!

這可能沒有幫助,但我知道操縱狀態代碼的唯一方法是引發異常。 提供了一組默認的異常,映射到400,401,403,404和500.文檔說你可以子類化endpoints.ServiceException來生成其他狀態代碼,但是我無法讓它工作。 如果將http_status設置為上面列出的其他內容之外的任何內容,則總是會產生400。

class TestException(endpoints.ServiceException):
    http_status = httplib.NO_CONTENT

我在我的處理程序中運行測試,如下所示:

raise TestException('The status should be 204')

我在使用API​​資源管理器測試時看到了這個輸出:

400 Bad Request

- Show headers -

{
 "error": {
  "errors": [
   {
    "domain": "global",
    "reason": "badRequest",
    "message": "The status should be 204"
   }
  ],
  "code": 400,
  "message": "The status should be 204"
 }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM