向App Engine(标准)上运行的Cloud Endpoints API发出HTTP请求时,服务器出错。 Stackdriver日志显示:
InvalidResponseError: status must be a str, got 'unicode' (u'403 Requests from referer <empty> are blocked.')"
我正在使用自动生成的Angular2,Typescript客户端,使用Swagger's online editor生成。生成的代码在发出请求时使用以下Angular2模块:
import { Http, Headers, URLSearchParams } from '@angular/http';
import { RequestMethod, RequestOptions, RequestOptionsArgs } from '@angular/http';
import { Response, ResponseContentType } from '@angular/http';
一切都适用于Chrome,但不适用于Firefox和IE。作为实验,我在Angular2客户端发出HTTP请求之前删除了任何标头(headers:undefined
)
let requestOptions: RequestOptionsArgs = new RequestOptions({
method: RequestMethod.Post,
headers: undefined,
body: body == null ? '' : JSON.stringify(body),
search: queryParameters
});
this.http.request(path, requestOptions);
这解决了问题,Firefox和IE工作正常。在将headers
设置为undefined
之前,添加的仅标题为:
headers.set('Content-Type', 'application/json');
查看Chrome和Firefox的HTTP标头,Firefox缺少Referer
Firefox请求标头(缺少Referer
)
Host: api.endpoints.myapp.appspot.com
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:51.0) Gecko/20100101 Firefox/51.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Access-Control-Request-Method: POST
Access-Control-Request-Headers: content-type
Origin: http://localhost:4200
Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache
Chrome请求标头
Accept:*/*
Accept-Encoding:gzip, deflate, sdch
Accept-Language:en-US,en;q=0.8
Access-Control-Request-Headers:content-type
Access-Control-Request-Method:POST
Connection:keep-alive
Host:api.endpoints.myapp.appspot.com
Origin:http://localhost:4200
Referer:http://localhost:4200/editor
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36
由于Google App Engine API使用API密钥和HTTP引荐来源白名单限制访问:
...空引用者导致HTTP请求被拒绝。问题: