Symfony JsonResponse忽略已定义的HTTP代码,仅响应200

时间:2017-05-22 16:38:25

标签: php nginx silex

我正在尝试在Response中更改HTTP代码,但我总是收到200。 两个测试都恢复了以下代码:

$app->put('/hdc/v1/pagamento/{pagamento_id}', function($pagamento_id, Request $request) use ($app) {

    $pagamento = Pagamento::find($pagamento_id);

    return new JsonResponse(["message"=>"error"],400); // returns 200
}




$app->put('/hdc/v1/pagamento/{pagamento_id}', function($pagamento_id, Request $request) use ($app) {

//    $pagamento = Pagamento::find($pagamento_id);

    return new JsonResponse(["message"=>"error"],400); // returns 400
}

与模型交互,JsonResponse受到影响。为什么呢?

Postman screenshot

2 个答案:

答案 0 :(得分:0)

我认为你没有使用好的方法。请参阅silex doc

gateway = boto3.client('apigateway', region_name=self.conf.region)
gateway.put_method(
     restApiId=apiId,
     resourceId=resourceId,
     httpMethod="OPTIONS",
     authorizationType="NONE"
)

gateway.put_method_response(
 restApiId=apiId,
 resourceId=resourceId,
 httpMethod="OPTIONS",
 statusCode="200",
 responseParameters={
   'method.response.header.Access-Control-Allow-Headers': True,
   'method.response.header.Access-Control-Allow-Origin': True,
   'method.response.header.Access-Control-Allow-Methods': True 
},
responseModels={
  'application/json': 'Empty'
}
   )

gateway.put_integration(
    restApiId=api['id'],
    resourceId=apiResource['id'],
    httpMethod="OPTIONS",
    type="MOCK",
    requestTemplates={
        'application/json': '{"statusCode": 200}'
 }
)

gateway.put_integration_response(
    restApiId=api['id'],
    resourceId=apiResource['id'],
    httpMethod="OPTIONS",
    statusCode="200",
    selectionPattern=".*",
    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": "'*'",
     "method.response.header.Access-Control-Allow-Origin": "'*'"
    },
    responseTemplates={
     'application/json': ''
    }
)  

gateway.put_method_response(
  restApiId=apiId,
  resourceId=resourceId,
  httpMethod="POST",
  statusCode=200,
  responseParameters={'method.response.header.Access-Control-Allow-Origin': True},
  responseModels={'application/json': 'Empty'}

答案 1 :(得分:0)

尴尬!

我的实体档案Pagamento

?>

在文件底部。 它在调用Pagamento :: find方法时发送了200个代码头。