0%

form-data 以及 basic auth

Postman
postform-data-auth
postform-data
Node.js Server

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
const express = require('express');
const app = express();
var multer = require('multer');
var upload = multer();

// Parse URL-encoded bodies (as sent by HTML forms)
app.use(express.urlencoded());

// for parsing multipart/form-data
app.use(upload.array());
app.use(express.static('public'));
app.post('/', function(request, response){
console.log(request.body);
response.json({res:'ok',data:request.body})
});

const listener = app.listen(process.env.PORT || 3000, function () {
console.log('Your app is listening on port ' + listener.address().port);
});

Node.js client
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
const http = require('http')
const FormData = require('form-data')

function btoa(str) {
return Buffer.from(str).toString('base64')
}
const formData = new FormData();
formData.append('username', 'QQs');
formData.append('password', btoa('*****'));
const authorizationData = 'Basic ' + btoa('authorizationID:authorizationPassword');
const options = {
hostname: 'localhost',
port: 3000,
path: '',
method: 'POST',
headers: formData.getHeaders(),
auth: authorizationData
};
const req = http.request(options, (res) => {
res.setEncoding('utf8');
res.on('data', (chunk) => {
console.log(`BODY: ${chunk}`);
});
res.on('end', () => {
console.log('No more data in response.');
});
});
req.on('error', (e) => {
console.error(`problem with request: ${e.message}`);
});
formData.pipe(req);

Angular httpclient
1
2
3
4
5
6
7
8
9
10
11
12
13
const formData = new FormData();
formData.append('username', 'QQs');
formData.append('password', btoa('*****'));
const postUrl = 'http://loacalhost:3000/';
const authorizationData = 'Basic ' + btoa('authorizationID:authorizationPassword');
const headers = new HttpHeaders({
'Authorization': authorizationData
});
return this.http.post(postUrl, postData, { headers: headers }).pipe(
map(response => {

})
)

html form
1
2
3
4
5
<form action="http://authorizationID:authorizationPassword@localhost:3000/" method="post" enctype="multipart/form-data">
<input type="text" name="username" id="username">
<input type="text" name="password" id="password">
<input type="submit" value="Post">
</form>