【发布时间】:2018-07-19 14:32:24
【问题描述】:
我正在使用 Angular 5 HttpClient,我正在尝试从提供 json 数据和 HTML 的页面获取 HTML 响应。 (Spotify 身份验证)。
当我 curl 时,我得到了预期的 HTML 和 json 有效负载。无论我用 HttpClient 尝试什么,我所能得到的都是 json,在这种情况下它没有帮助。我想访问 HTML。我已经验证我的标题在命令行 curl 和 HttpClient 之间是相同的。
curl -vvv https://accounts.spotify.com/authorize/?client_id=xxxxxxxxxxxxxxx81ad8fc&response_type=code&redirect_uri=http://reaver.xxxxxx.com:4200/callback&state=34fFs29kd09
<!DOCTYPE html>
<html ng-app="accounts" ng-csp>
<head>
<meta charset="utf-8">
<title ng-bind="(title && (title | localize) + ' - ') + 'Spotify'">Spotify</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<base href="/">
<link href="https://d2d1dxiu3v1f2i.cloudfront.net/a4a5157/css/index.css" media="screen" rel="stylesheet">
<script async defer src="https://www.google.com/recaptcha/api.js"></script>
<script async defer src="https://d2d1dxiu3v1f2i.cloudfront.net/a4a5157/js/index.js" sp-bootstrap></script>
<meta ng-non-bindable sp-bootstrap-data='{"client":{"name":"Playlist Reaver"},"country":"US","useCaptcha":false,"locales":["*"],"BON":["0","0",-795429514]}'>
</head>
<body ng-view></body>
</html>
该有效负载的这一部分是我可以让 HttpClient 向我公开的全部内容。
{"client":{"name":"Playlist Reaver"},"country":"US","useCaptcha":false,"locales":["*"],"BON":["0","0",-795429514]}
通常我会说这很好,但我确实需要访问 HTML。
如何从包含 json 数据和 HTML 的此类响应中获取原始 html?
我的 get 调用如下所示:
return this.http.get(this.apiGeneric, { params: params, observe: 'response'});
附加信息:似乎没有添加我的 http 标头。我进行了以下更改,并且在 http 请求标头中没有看到 XFF 标头。
// function returns Observable UserResponse object
getUrl() {
this.httpHeaders = new HttpHeaders();
//.set('Content-Type'
this.httpHeaders.set('Accept', 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8');
this.httpHeaders.set('XFF', 'testing123');
let params = new HttpParams();
const params = new HttpParams()
.set('client_id', this.clientId)
.set('response_type', 'code')
.set('redirect_uri', this.redirectUri)
console.log(params);
return this.http.get(this.apiGeneric, { headers: this.httpHeaders, params: params, observe: 'response' });
}
【问题讨论】:
-
您是否尝试过设置合适的接受类型?
-
尝试使用 { params: params, observe: 'response', responseType: 'text'}
-
谢谢@jonrsharpe。我确实做到了: this.httpHeaders.set('Accept', 'text/html'); Chrome 开发者模式将此捕获为正在发送的标头:Accept:application/json, text/plain, /
-
@david 谢谢。一旦我获得了正确的 api 标头并获得了 HTML 和其他有效负载,然后我得到了 json 解码错误,并且 responseType 帮助关闭了 json 解码。一切就绪,谢谢!
标签: angular httpclient spotify