aurelia-fetch-client即时创建请求头 [英] aurelia-fetch-client create request headers on the fly
问题描述
我使用aurelia-fetch-client将一些数据发送到一个web-api(在一个注册方法中)。
标题:标题;
register(){
this.headers = new Headers();
this.headers.append(content-type,application / json; charset = utf-8);
this.httpClient.fetch(api / Account / Register,{
method:POST,
body:JSON.stringify({
email: this.email,
password:this.password
}),
headers:this.headers
})
}
code>
正如你所看到的,我想更新我的请求头文件(在append方法调用中),为此我需要创建我自己的Headers对象,调用附加在其上的方法,然后将其分配给我的请求的headers属性。我想直接在请求体中执行该操作:而不是写入
头文件:this.headers
我想写下如下内容:
append(content-type,application / json; charset = utf-8);
或类似的东西:
headers:new Headers()。append(..)
这个想法是为了避免声明一个新的对象来存储我的头文件。
我该怎么做?
谢谢你。
您可以直接将带有键和值的JS对象文本传递给头
属性:
< pre $
this.httpClient.fetch(api / Account / Register,{
method:POST,
body:JSON.stringify({
电子邮件:this.email,
密码:this.password
}),
标题:{
content-type,application / json; charset = utf-8
}
});
或者您也可以创建 Headers
对象预填充您的自定义标题:
this.httpClient.fetch(api / Account / Register,{
方法:POST,
body:JSON.stringify({
email:this.email,
password:this.password
}),
headers:new Headers({
content-type,application / json; charset = utf-8
})
});
另见插件相关测试的头文件。
I am using aurelia-fetch-client to send some data to a web-api (in a register method).
headers: Headers;
register() {
this.headers = new Headers();
this.headers.append("content-type", "application/json; charset=utf-8");
this.httpClient.fetch("api/Account/Register", {
method: "POST",
body: JSON.stringify({
email: this.email,
password: this.password
}),
headers: this.headers
})
}
As you see, I want to update the headers of my request (in that append method call) and for doing that I need to create my own Headers object, to call the method append on it and then to assign it to the headers property of my request. I want to do that directly in the request body: instead of writing
headers: this.headers
I want to write something like:
headers: {
append("content-type", "application/json; charset=utf-8");
}
or something like:
headers: new Headers().append(..)
The idea is to avoid declaring a new object for storing my headers. How can I do that?
Thank you respectfully.
You can just pass in an JS object literal with the keys and values directly to the headers
property:
this.httpClient.fetch("api/Account/Register", {
method: "POST",
body: JSON.stringify({
email: this.email,
password: this.password
}),
headers: {
"content-type", "application/json; charset=utf-8"
}
});
Or you can also crate the Headers
object pre-filled with your custom headers:
this.httpClient.fetch("api/Account/Register", {
method: "POST",
body: JSON.stringify({
email: this.email,
password: this.password
}),
headers: new Headers({
"content-type", "application/json; charset=utf-8"
})
});
See also the headers related test of the plugin.
这篇关于aurelia-fetch-client即时创建请求头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!