我正试图通过Zapier使用fetch在Code中形成对Pastebin API的post请求。经过一番尝试和错误之后,我设法使用URLencoded数据完成了这项工作,如下所示:
const { URLSearchParams } = require('url');
const encodedParams = new URLSearchParams();
encodedParams.set('api_paste_code', 'test');
encodedParams.set('api_option', 'paste');
encodedParams.set('api_dev_key', '1NLa6pHlhuBYTMAb1GDiAYHa1xAy5Ihd');
let url = 'https://pastebin.com/api/api_post.php';
let options = {
method: 'POST',
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
body: encodedParams.toString()
};
const fetchResult = await fetch(url, options);
output = {output: fetchResult}
我得到了200的回复,但我收到的数据是我一生中从未见过的。它是这样的:
output
url
https://pastebin.com/api/api_post.php
status
200
statusText
OK
headers
_headers
date
1
Fri, 08 Apr 2022 13:45:23 GMT
content-type
1
text/html; charset=UTF-8
transfer-encoding
1
chunked
connection
1
close
x-custom-api-dev-id
1
8604170
set-cookie
1
pastebin_posted=49abfc325ca96e5daf3e6b81de576fbdae7e584ab1dc300294f770a2200d6771a%3A2%3A%7Bi%3A0%3Bs%3A15%3A%22pastebin_posted%22%3Bi%3A1%3Bs%3A8%3A%22X6WmZQ3u%22%3B%7D; expires=Fri, 08-Apr-2022 14:45:23 GMT; Max-Age=3600; path=/; HttpOnly
content-encoding
1
gzip
cf-cache-status
1
DYNAMIC
expect-ct
1
max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct"
server
1
cloudflare
cf-ray
1
6f8b716dfe295790-IAD
ok
true
body
_writeState
0
0
1
0
_readableState
objectMode
false
highWaterMark
16384
buffer
head
null
tail
null
length
0
length
0
pipes
null
pipesCount
0
flowing
null
ended
false
endEmitted
false
reading
false
sync
false
needReadable
false
emittedReadable
false
readableListening
false
resumeScheduled
false
emitClose
true
autoDestroy
false
destroyed
false
defaultEncoding
utf8
awaitDrainWriters
null
multiAwaitDrain
false
readingMore
false
decoder
null
encoding
null
readable
true
_events
_eventsCount
6
_writableState
objectMode
false
highWaterMark
16384
finalCalled
false
needDrain
false
ending
false
ended
false
finished
false
destroyed
false
decodeStrings
true
defaultEncoding
utf8
length
49
writing
true
corked
0
sync
false
bufferProcessing
false
writelen
49
afterWriteTickInfo
null
bufferedRequest
null
lastBufferedRequest
null
pendingcb
1
prefinished
false
errorEmitted
false
emitClose
true
autoDestroy
false
bufferedRequestCount
0
corkedRequestsFree
next
null
entry
null
writable
true
allowHalfOpen
true
_transformState
needTransform
false
transforming
true
writechunk
type
Buffer
data
1
31
2
139
3
8
4
0
5
0
6
0
7
0
8
0
9
0
10
3
11
203
12
40
13
41
14
41
15
40
16
182
17
210
18
215
19
47
20
72
21
44
22
46
23
73
24
77
25
202
26
204
27
211
28
75
29
206
30
207
31
213
32
143
33
48
34
11
然后,它继续用这些行的数字表示数千行。我尝试使用decodeURI组件,但返回的值是[object object],我无法将其解析为字符串。
我怀疑在发送之前将encodedParams解析为字符串可能会导致问题,因为在我的IDE中的Node和poster中,我不使用toString((,并且我对粘贴的url有正确的响应。但在Zappier中,如果我使用相同的代码;不可处理实体";。只有在将encodedParams解析为字符串之后,我才设法获得响应代码200。我被卡住了。
我希望我的问题格式可以,这是我第一次在StackOverflow上。谢谢
这实际上是有效的,您可以在响应中看到您的状态为200
,问题是,您没有从HTTP响应中提取数据。
您需要进行此更改才能查看实际数据:
const fetchResult = await fetch(url, options);
const data = await fetchResult.json();
output = {output: data}
如果您愿意,也可以将.json()
替换为.text()
。
我希望这能有所帮助!