Skip to content

Commit 29f7817

Browse files
authored
Merge pull request #44 from coder-hxl/fix/43
fix(crawlData): correct handle the header of the post method
2 parents 892abb3 + ddb076c commit 29f7817

File tree

5 files changed

+31
-43
lines changed

5 files changed

+31
-43
lines changed

src/request.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,24 @@ function parseHeaders(
2828
) {
2929
const rawHeaders = rawConfig.headers ?? {}
3030
const headers: AnyObject = {
31-
'user-agent':
32-
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36',
31+
'User-Agent':
32+
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36',
3333
...rawHeaders
3434
}
3535

3636
if (config.method === 'POST' && rawConfig.data) {
37-
headers['Content-Type'] = 'application/json'
38-
headers['Content-Length'] = Buffer.byteLength(rawConfig.data)
37+
const defaultHeaderConfig = [
38+
{ key: 'Content-Type', value: 'application/json' },
39+
{ key: 'Content-Length', value: Buffer.byteLength(rawConfig.data) }
40+
]
41+
42+
defaultHeaderConfig.forEach((item) => {
43+
const { key, value } = item
44+
45+
if (isUndefined(rawHeaders[key])) {
46+
headers[key] = value
47+
}
48+
})
3949
}
4050

4151
return headers

test/server/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
"use strict";require("node:http").createServer(((e,t)=>{t.setHeader("Content-Type","text/plain"),t.end("success")})).listen(8888,(()=>{console.log("服务器在 8888 端口启动成功~")}));
1+
"use strict";require("node:http").createServer(((e,t)=>{console.log(e.headers),t.setHeader("Content-Type","text/plain"),t.end("success")})).listen(8888,(()=>{console.log("服务器在 8888 端口启动成功~")}));

test/server/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import http from 'node:http'
22

33
http
44
.createServer((req, res) => {
5+
console.log(req.headers)
6+
57
res.setHeader('Content-Type', 'text/plain')
68
res.end('success')
79
})

test/start/index.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/start/index.ts

Lines changed: 13 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,16 @@
11
import xCrawl from 'x-crawl'
22

3-
const testXCrawl = xCrawl({
4-
baseUrl: 'http://8.210.98.225:9001/api/room/193581217/room_picture'
5-
})
3+
const testXCrawl = xCrawl()
64

7-
const names = [
8-
'1672468265289r193581217',
9-
'1672468265300r193581217',
10-
'1672468265284r193581217',
11-
'1672468265279r193581217',
12-
'1672468265298r193581217',
13-
'1672468265293r193581217'
14-
]
15-
16-
const targets: string[] = []
17-
const fileNames: (string | null)[] = []
18-
const storeDirs: string[] = []
19-
const extensions: (string | null)[] = []
20-
21-
names.forEach((name, i) => {
22-
targets.push(`/${name}.jpg`)
23-
24-
if (i % 2) {
25-
fileNames.push(name)
26-
storeDirs.push(`./upload/${name}`)
27-
extensions.push('.jpg')
28-
} else {
29-
fileNames.push(null)
30-
storeDirs.push('./upload')
31-
extensions.push(null)
32-
}
33-
})
34-
35-
testXCrawl.crawlFile({
36-
targets,
37-
storeDirs,
38-
fileNames,
39-
extensions
40-
})
5+
testXCrawl
6+
.crawlData({
7+
url: 'http://localhost:8888',
8+
method: 'post',
9+
headers: {
10+
'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
11+
},
12+
data: { name: 'hxl', age: 19 }
13+
})
14+
.then((res) => {
15+
console.log(res)
16+
})

0 commit comments

Comments
 (0)