2016-06-27 9 views
1

私はAWS Api GatewayのCORSを構成するのに何時間も前から試みてきました。私は、 "CORSを有効にする"ボタンがawsコンソール内で何をしているのかを逐語的にコピーしようとしました。しかし、すべてのメソッドがコンソールで同じに見えても、残りのAPIへのPOSTは「CORSを有効にする」ボタンで機能しますが、CORSが自分のコードを使用して設定されていると、AWS Api Gateway用BOTO3を使用したCORSの自動化

これは、CORSの設定に関連するコードです:

# Set the put method response of the POST method 
self.apigateway.put_method_response(
    restApiId=self.rest_api['id'], 
    resourceId=root_resource['id'], 
    httpMethod='POST', 
    statusCode='200', 
    responseParameters={ 
     'method.response.header.Access-Control-Allow-Origin': False 
    }, 
    responseModels={ 
     'application/json': 'Empty' 
    } 
) 

# Set the put integration response of the POST method 
self.apigateway.put_integration_response(
    restApiId=self.rest_api['id'], 
    resourceId=root_resource['id'], 
    httpMethod='POST', 
    statusCode='200', 
    responseParameters={ 
     'method.response.header.Access-Control-Allow-Origin': '\'*\'' 
    }, 
    responseTemplates={ 
     'application/json': '' 
    } 
) 

# Add an options method to the rest api 
api_method = self.apigateway.put_method(
    restApiId=self.rest_api['id'], 
    resourceId=root_resource['id'], 
    httpMethod='OPTIONS', 
    authorizationType='NONE' 
) 

# Set the put integration of the OPTIONS method 
self.apigateway.put_integration(
    restApiId=self.rest_api['id'], 
    resourceId=root_resource['id'], 
    httpMethod='OPTIONS', 
    type='MOCK', 
    requestTemplates={ 
     'application/json': '' 
    } 
) 

# Set the put method response of the OPTIONS method 
self.apigateway.put_method_response(
    restApiId=self.rest_api['id'], 
    resourceId=root_resource['id'], 
    httpMethod='OPTIONS', 
    statusCode='200', 
    responseParameters={ 
     'method.response.header.Access-Control-Allow-Headers': False, 
     'method.response.header.Access-Control-Allow-Origin': False, 
     'method.response.header.Access-Control-Allow-Methods': False 
    }, 
    responseModels={ 
     'application/json': 'Empty' 
    } 
) 

# Set the put integration response of the OPTIONS method 
self.apigateway.put_integration_response(
    restApiId=self.rest_api['id'], 
    resourceId=root_resource['id'], 
    httpMethod='OPTIONS', 
    statusCode='200', 
    responseParameters={ 
     'method.response.header.Access-Control-Allow-Headers': '\'Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token\'', 
     'method.response.header.Access-Control-Allow-Methods': '\'POST,OPTIONS\'', 
     'method.response.header.Access-Control-Allow-Origin': '\'*\'' 
    }, 
    responseTemplates={ 
     'application/json': '' 
    } 
) 

CORSをAWSコンソールを介して有効になっているときにPOSTとOPTIONSのためのgetメソッドからの応答である:

{ 
    "httpMethod": "POST", 
    "apiKeyRequired": false, 
    "methodIntegration": { 
     "httpMethod": "POST", 
     "cacheKeyParameters": [], 
     "integrationResponses": { 
      "200": { 
       "responseParameters": { 
        "method.response.header.Access-Control-Allow-Origin": "'*'" 
       }, 
       "statusCode": "200", 
       "responseTemplates": { 
        "application/json": null 
       } 
      } 
     }, 
     "uri": "arn:aws:apigateway:us-east-1:lambda:path/2015-03-31/functions/arn:aws:lambda:us-east-1:477869670267:function:controller/invocations", 
     "requestTemplates": { 
      "application/json": null 
     }, 
     "cacheNamespace": "o9h9b8tzo2", 
     "type": "AWS" 
    }, 
    "methodResponses": { 
     "200": { 
      "responseParameters": { 
       "method.response.header.Access-Control-Allow-Origin": false 
      }, 
      "statusCode": "200", 
      "responseModels": { 
       "application/json": "Empty" 
      } 
     } 
    }, 
    "authorizationType": "NONE" 
} 
{ 
    "requestParameters": {}, 
    "httpMethod": "OPTIONS", 
    "methodResponses": { 
     "200": { 
      "statusCode": "200", 
      "responseParameters": { 
       "method.response.header.Access-Control-Allow-Headers": false, 
       "method.response.header.Access-Control-Allow-Methods": false, 
       "method.response.header.Access-Control-Allow-Origin": false 
      }, 
      "responseModels": { 
       "application/json": "Empty" 
      } 
     } 
    }, 
    "apiKeyRequired": false, 
    "methodIntegration": { 
     "cacheNamespace": "o9h9b8tzo2", 
     "type": "MOCK", 
     "requestTemplates": { 
      "application/json": "{\"statusCode\": 200}" 
     }, 
     "integrationResponses": { 
      "200": { 
       "responseTemplates": { 
        "application/json": null 
       }, 
       "statusCode": "200", 
       "responseParameters": { 
        "method.response.header.Access-Control-Allow-Headers": "'Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token'", 
        "method.response.header.Access-Control-Allow-Methods": "'POST,OPTIONS'", 
        "method.response.header.Access-Control-Allow-Origin": "'*'" 
       } 
      } 
     }, 
     "cacheKeyParameters": [] 
    }, 
    "authorizationType": "NONE" 
} 

これは私のコードを使用してCORSからのget-methodの応答を有効にしています:

{ 
    "authorizationType": "NONE", 
    "httpMethod": "POST", 
    "methodIntegration": { 
     "requestTemplates": { 
      "application/json": null 
     }, 
     "cacheNamespace": "308o168qal", 
     "uri": "arn:aws:apigateway:us-east-1:lambda:path/2015-03-31/functions/arn:aws:lambda:us-east-1:477869670267:function:controller/invocations", 
     "httpMethod": "POST", 
     "cacheKeyParameters": [], 
     "integrationResponses": { 
      "200": { 
       "responseParameters": { 
        "method.response.header.Access-Control-Allow-Origin": "'*'" 
       }, 
       "statusCode": "200", 
       "responseTemplates": { 
        "application/json": null 
       } 
      } 
     }, 
     "type": "AWS" 
    }, 
    "apiKeyRequired": false, 
    "methodResponses": { 
     "200": { 
      "responseParameters": { 
       "method.response.header.Access-Control-Allow-Origin": false 
      }, 
      "responseModels": { 
       "application/json": "Empty" 
      }, 
      "statusCode": "200" 
     } 
    } 
} 
{ 
    "authorizationType": "NONE", 
    "apiKeyRequired": false, 
    "methodIntegration": { 
     "integrationResponses": { 
      "200": { 
       "statusCode": "200", 
       "responseTemplates": { 
        "application/json": null 
       }, 
       "responseParameters": { 
        "method.response.header.Access-Control-Allow-Methods": "'POST,OPTIONS'", 
        "method.response.header.Access-Control-Allow-Origin": "'*'", 
        "method.response.header.Access-Control-Allow-Headers": "'Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token'" 
       } 
      } 
     }, 
     "cacheNamespace": "bm4zmvzkdk", 
     "type": "MOCK", 
     "cacheKeyParameters": [], 
     "requestTemplates": { 
      "application/json": "{\"statusCode\": 200}" 
     } 
    }, 
    "requestParameters": {}, 
    "methodResponses": { 
     "200": { 
      "statusCode": "200", 
      "responseModels": { 
       "application/json": "Empty" 
      }, 
      "responseParameters": { 
       "method.response.header.Access-Control-Allow-Methods": false, 
       "method.response.header.Access-Control-Allow-Origin": false, 
       "method.response.header.Access-Control-Allow-Headers": false 
      } 
     } 
    }, 
    "httpMethod": "OPTIONS" 
} 

一つの違いは見えませんが、私は何が間違っていますか?

POSTリクエストは、S3の中に座って、ファイル内のJavaScriptで作られているAWSでMikeDの要求ごととして:

$(endpoint_url)と$(API_KEYが)私で適切な値に置き換えられ
function post_request() { 
    var xhr = new XMLHttpRequest(); 
    var params = JSON.stringify({ 
     request: "registerUser", 
     user:{ 
      username: document.getElementById("usernameInput").value, 
      email: document.getElementById("emailInput").value, 
      password: document.getElementById("passwordInput").value 
     } 
    }); 
    xhr.open("POST", "$(endpoint_url)", true); 
    xhr.setRequestHeader("Content-type", "application/json"); 
    xhr.setRequestHeader("x-api-key", "$(api_key)"); 
    xhr.onreadystatechange = function(){ 
     if(xhr.readyState === 4){ 
      if(xhr.status === 200){ 
       alert("You are registered!"); 
      } 
      else{ 
       alert("Could not register. Please try again later."); 
      } 
     } 
    }; 
    xhr.send(params); 
    return false; 
} 

セットアップスクリプト(私は値が正確であることを確認しました)。 POSTリクエストが行われたときに

これはクロムコンソールからの逐語的な応答である:

register.html?X-Amz-Date=20160628T070211Z&X-Amz-Expires=300&X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-…:39 OPTIONS https://dn9sjxz0i9.execute-api.us-east-1.amazonaws.com/prod post_request @ register.html?X-Amz-Date=20160628T070211Z&X-Amz-Expires=300&X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-…:39document.getElementById.onsubmit @ register.html?X-Amz-Date=20160628T070211Z&X-Amz-Expires=300&X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-…:44 
register.html?X-Amz-Date=20160628T070211Z&X-Amz-Expires=300&X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-…:1 XMLHttpRequest cannot load https://dn9sjxz0i9.execute-api.us-east-1.amazonaws.com/prod. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'https://s3.amazonaws.com' is therefore not allowed access. The response had HTTP status code 500. 
+0

*「鉱山あなたがAPIゲートウェイを介して統合を作成すると、それはデフォルトのマッピングテンプレートを追加します動作しません "*実際に私たちに何かを与えることはありません。どのような意味では機能しませんか?私がここに見るのは構成です。観察された動作や期待される動作の説明の例ではありません。 'curl'のようなツールでリクエストヘッダとレスポンスヘッダを取得しましたか?結果は何でしたか? –

+0

申し訳ありませんまだ私はまだかなり新しいです。私は何が間違っていたか(500エラー)とAPI Gatewayのget-methodの出力を明確にしました。フィードバックありがとう:) –

+0

どのようにAPIを呼び出していますか? curlであれば、OPTIONS呼び出しとPOST呼び出しの詳細な要求/応答の詳細を投稿できます。ブラウザであれば、ブラウザが行ったすべてのログを投稿できますか? (ほとんどのブラウザには、ブラウザが作成するCORSリクエストとそのレスポンスを表示するツールがあります)。 –

答えて

0

OPTIONSメソッドのプット・統合が200値からstatusCodeを含んマッピングテンプレートを持っている必要があります。あなたのコードがマッピングテンプレートを空の文字列( '')に設定していたようです。 {「からstatusCode」:200}

はそうのようなあなたのプット・統合に同じマッピングテンプレートを追加します。

# Set the put integration of the OPTIONS method 
self.apigateway.put_integration(
    restApiId=self.rest_api['id'], 
    resourceId=root_resource['id'], 
    httpMethod='OPTIONS', 
    type='MOCK', 
    requestTemplates={ 
     'application/json': '{"statusCode": 200}' 
    } 
) 
関連する問題