移动了Yii2 REST API IIS对象

时间:2017-04-25 18:13:42

标签: windows rest api iis yii2

我正在使用带有 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"}

3 个答案:

答案 0 :(得分:1)

我遇到了类似的问题。它似乎与FastCGI有关。不确定。我知道将响应标头设置为201 http状态代码(this line in source code)时会发生这种情况,以后会被IIS更改。如果您有权访问服务器,请尝试以下解决方案:

  

W7 Pro IIS 7.5 overwrites PHP Location: Header (solved)

在我的情况下,我只有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="^\&lt;head\&gt;.*?$" />
        <action type="Rewrite" value="" />
    </rule>
    <rule name="Remove the tag BODY" preCondition="isStatus201">
        <match filterByTags="None" pattern="^\&lt;body\&gt;.*?\&lt;/body\&gt;" />
        <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]*)*.*&lt;/body>" />
    <action type="Rewrite" value="" />
  </rule>
  <preConditions>
   <preCondition name="Status 201" patternSyntax="Wildcard">
     <add input="{RESPONSE_STATUS}" pattern="201" ignoreCase="false" />
   </preCondition>
  </preConditions>
</outboundRules>