我正在使用angular 5并使用XML数据发出请求(因为我的后端只接受XML格式的数据)。我想将标题的Content-Type更改为application / xml,但它总是发送带有text / plan的请求。请仔细研究,让我知道我做错了什么。
这是我的代码
import { Injectable } from '@angular/core';
import { HttpClient , HttpHeaders, HttpRequest } from '@angular/common/http';
import { Response, Http, RequestOptions ,Headers } from '@angular/http';
import { Observable } from 'rxjs';
import 'rxjs/add/operator/catch';
import 'rxjs/add/observable/throw';
import 'rxjs/Rx';
import * as xml2js from 'xml2js';
@Injectable()
export class ApiService {
constructor( private httpClient: HttpClient, private http: Http ) { }
signin(){
let header = new HttpHeaders();
header.append('Content-Type', 'application/xml');
header.append('Accept' , 'application/xml');
console.log("Checking Content-Type: " , header.get('Content-Type'));
let body = '<request>' +
'<username>Ken</username>' +
'<password>sparks</password>' +
'</request>';
return this.httpClient.post('https://Ip:8002/?event=account_login', body , {headers:header});
}
}
这是我的网络
答案 0 :(得分:1)
尝试将代码更改为
let header = new HttpHeaders();
header = header.append('Content-Type', 'application/xml');
header = header.append('Accept' , 'application/xml');
答案 1 :(得分:1)
HttpHeaders
是不可变的。
https://angular.io/guide/http#update-headers
请尝试使用标题。
let header = new HttpHeaders()
.append('Content-Type', 'application/xml')
.append('Accept' , 'application/xml');
另外,如果您不希望自动将响应解析为Json,则需要指定responseType
return this.httpClient.post('https://Ip:8002/?event=account_login', body , {headers:header, responseType: 'text'});