【发布时间】:2018-05-07 12:39:04
【问题描述】:
我正在使用 Angular 6 httpClient 并在服务中有此代码:
import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
const httpOptions = {
headers: new HttpHeaders({ 'Content-Type': 'text/xml' })
};
@Injectable({
providedIn: 'root'
})
export class ApiService {
constructor(private http: HttpClient) { }
post() {
const postedData = { userid: 1, title: 'title here', body: 'body text' };
return this.http.post('this url here', postedData, httpOptions).subscribe(result => {
console.log(result);
}, error => console.log('There was an error: '));
}
}
我的问题是:我想发布一个 xml 文件,那么我该如何修改这段代码呢?
【问题讨论】:
-
只需将
postedData更改为您的 XML 字符串 -
添加 'content-type' 标头和可选的 'accept'
const headers = new HttpHeaders(); headers = headers.append('Content-Type': 'text/xml'); headers = headers.append('Accept', 'text/xml');然后将您的有效负载作为字符串发送。
标签: angular typescript angular6