httpPost 发起 HTTP POST 请求
httpPost() 发起 HTTP POST 请求
luwak.http.post(url: string, data: any, config?: object);
- 自动检测
Content-Type
- 自动编码解码 JSON 内容
何时使用
- 当需要通过 HTTP 与外部系统交互时
请求配置
只有 url 参数是必选的,data 和 config 参数是可选的。
{
// 自定义请求头
headers: {'X-Requested-With': 'LuwakHttpRequest'},
// `params` 是与请求一起发送的 URL 参数
// 必须是一个简单对象
params: {
ID: 12345
},
// `data` 是作为请求体被发送的数据, 必须是以下类型之一:
// - string, plain object, array
data: {
firstName: 'Fred'
},
}
响应结构
一个请求的响应包含以下信息。
{
// `data` 由服务器提供的响应
data: {},
// `status` 来自服务器响应的 HTTP 状态码
status: 200,
// `statusText` 来自服务器响应的 HTTP 状态信息
statusText: 'OK',
// `headers` 是服务器响应头
// 所有的 header 名称都是小写,而且可以使用方括号语法访问
// 例如: `response.headers['content-type']`
headers: {},
}
代码示例
简单请求
var post = {
"userId": 1,
"id": 1,
"title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit",
"body": "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto"
};
// 取出响应结构中的 data 属性
var { data } = luwak.http.post("https://jsonplaceholder.typicode.com/posts", post);
luwak.returnResult(data);
请求参数
同 httpGet()
请求头
同 httpGet()