我正在使用带有 ActiveController 的Yii2 REST创建新的Pessoa(),在Apache上工作正常,但在IIS 8上发生错误。
有没有人知道IIS中的任何配置?
REQUEST
Request URL:http://10.192.1.145/api/pessoa
Request Method:POST
Status Code:201 Created
Remote Address:10.192.1.145
Referrer Policy:no-referrer-when-downgrade
RESPONSE
<head><title>Document Moved</title></head>
<body><h1>Object Moved</h1>This document may be found
<a HREF="http://10.192.1.145/api/pessoa/45">here</a></body>{"id":"21"}
答案 0 :(得分:1)
我遇到了类似的问题。它似乎与FastCGI有关。不确定。我知道将响应标头设置为201 http状态代码(this line in source code)时会发生这种情况,以后会被IIS更改。如果您有权访问服务器,请尝试以下解决方案:
在我的情况下,我只有FTP访问服务器,所以我通过以下内容覆盖Create Action,强制200状态代码而不是201:
public function actions()
{
$actions = parent::actions();
unset($actions['create']);
return $actions;
}
public function actionCreate() {
$model = new Pessoa();
$model->load(Yii::$app->getRequest()->getBodyParams(), '');
if ($model->save() === false && !$model->hasErrors()) {
throw new ServerErrorHttpException('Failed to update the object for unknown reason.');
}
return $model;
}
答案 1 :(得分:1)
以防万一其他人需要保留状态代码并仅删除IIS添加的响应部分,这就是我为解决问题所做的工作。不过,您可能需要根据自己的需要进行调整:
<!-- PS: In my case, i just had a one line HEAD and a one line BODY being added. -->
<outboundRules>
<rule name="Remove the tag HEAD" preCondition="isStatus201">
<match filterByTags="None" pattern="^\<head\>.*?$" />
<action type="Rewrite" value="" />
</rule>
<rule name="Remove the tag BODY" preCondition="isStatus201">
<match filterByTags="None" pattern="^\<body\>.*?\</body\>" />
<action type="Rewrite" value="" />
</rule>
<preConditions>
<preCondition name="isStatus201">
<add input="{RESPONSE_STATUS}" pattern="^201$" />
</preCondition>
</preConditions>
</outboundRules>
在 IIS 8.5 中测试;
希望能帮到别人。
答案 2 :(得分:0)
事实上,我能够优化正则表达式,以便单个规则捕获结束正文标记的所有行:
<outboundRules>
<rule name="Remove injected 201 content" preCondition="Status 201">
<match filterByTags="None" pattern="^(?:.*[\r\n]*)*.*</body>" />
<action type="Rewrite" value="" />
</rule>
<preConditions>
<preCondition name="Status 201" patternSyntax="Wildcard">
<add input="{RESPONSE_STATUS}" pattern="201" ignoreCase="false" />
</preCondition>
</preConditions>
</outboundRules>