Skip to content

Commit 47e8c4d

Browse files
authored
feat: httpclient upload files example (#105)
1 parent 69864d5 commit 47e8c4d

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

httpclient/app/controller/httpclient.js

+22
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,28 @@ class HttpclientController extends Controller {
104104
ctx.body = result.data.files;
105105
}
106106

107+
// https://github.com/node-modules/urllib/pull/322
108+
async files() {
109+
const ctx = this.ctx;
110+
111+
const result = await ctx.curl('https://httpbin.org/post', {
112+
// 必须指定 method,支持 POST,PUT
113+
method: 'POST',
114+
// 上传文件
115+
files: {
116+
file1: __filename,
117+
file2: Buffer.from('mock file content'),
118+
},
119+
// 其他 Field
120+
data: {
121+
foo: 'bar',
122+
},
123+
// 明确告诉 httpclient 以 JSON 格式处理响应 body
124+
dataType: 'json',
125+
});
126+
ctx.body = result.data.files;
127+
}
128+
107129
async postStream() {
108130
const ctx = this.ctx;
109131

httpclient/app/router.js

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ module.exports = app => {
88
app.router.get('/delete', 'httpclient.del');
99
app.router.get('/form', 'httpclient.form');
1010
app.router.get('/multipart', 'httpclient.multipart');
11+
app.router.get('/files', 'httpclient.files');
1112
app.router.get('/stream', 'httpclient.stream');
1213
app.router.post('/stream', 'httpclient.postStream');
1314
app.router.get('/error', 'httpclient.error');

0 commit comments

Comments
 (0)