2017-12-29 22 views
1

私はそうのような詳細情報をヒットラムダ関数を伴うHTTPエンドポイントのシリーズを持っている:Serverless Frameworkを使用して、無効で不完全なエンドポイントに対する応答を作成しますか?

products/1 
users/1 
package/998134 

products/users/またはpackage/に対応するエンドポイントはありません。 users/{id}package/{id}エンドポイントの周りにカスタムオーソライザがあります。

GETを使用してusers/またはpackages/に行く場合は、{"message":"Missing Authentication Token"}を受信します。 (彼らはまた、IDを渡すとしたら、私のエンドポイントのために有効なオプション)彼らはPOSTでこれらのエンドポイントをヒットした場合、彼らは

{'message': "Authorization header requires 'Credential' parameter. Authorization header requires 'Signature' parameter. Authorization header requires 'SignedHeaders' parameter. Authorization header requires existence of either a 'X-Amz-Date' or a 'Date' header. Authorization=MYTOKENHERE"} 

は、これらのメッセージのいずれかを調整する方法はあり受け取りますか?私の最初に考えたのは、ちょうどその関連するエンドポイントを作成することでした。

GET users/ 
POST users/ 
GET products/ 
POST products/ 
GET package/ 
POST package 

をしかし、それはすぐに私が行うためにこれらのエンドポイントの3以上のものを持っているので、多くの仕事であることを取得します。私はまたそれらのいくつかにDELETEPUTを含める必要があります。

無効なエンドポイントまたは不完全なエンドポイントのレスポンスを簡単に作成できますか?

は私のserverless.ymlは定義は、現在、次のようになりました:

show_user: 
    handler: users/show_users.return_user 
    events: 
    - http: 
     path: users/{id} 
     method: get 
     cors: true 
update_user: 
    handler: users/update_user.update_user 
    events: 
    - http: 
     path: users/{id} 
     method: post 
     cors: true 
     authorizer: ${self:custom.authorizer.users} 

答えて

0

私は最近、似た何かをする必要がありました。

error_messages: 
    handler: custom_errors/errors.invalid_path 
    events: 
    - http 
     path: users/ 
     method: any 
    - http 
     path: product/ 
     method: any 
    - http 
     path: package/ 
     method: any 

次に、指定された場所にハンドラを作成し、必要な結果を返します。この場合、ハンドラはcustom_errors/errors.invalid_pathになります

私の場合は、{id}が必要であるというカスタムメッセージでHTTP 404を返しました。

method: any行はすべてのHTTP動詞を捕捉するので、個々のHTTP動詞のグループではなく、この保護を適用するエンドポイントごとに3行を追加するだけで済みます。

0

私はそうのようなApiGatewayの{プロキシ+}キャッチオール機能を使用して、これを達成:他の有効なエンドポイントがありませんように、あなたのサーバレスファイルの最後のHTTPエンドポイントもこのMUST

no-endpoint: 
    description: catch all non-existent enpoints with 404 - must be 
last http 
    handler: src/functions/api-controller.noEndpoint 
    events: 
     - http: 
      path: /{proxy+} 
      method: any 
      cors: true 

捕らえられた詳しくはこちらをご覧ください:

https://aws.amazon.com/blogs/aws/api-gateway-update-new-features-simplify-api-development/

関連する問題