【发布时间】:2022-07-29 14:05:35
【问题描述】:
我正在尝试通过 Zapier 使用 fetch 在 Code 中制定对 Pastebin API 的发布请求。经过一些试验和错误,我设法使用 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
然后它继续使用这些数字行数以千计的行。我尝试使用 decodeURIcomponent 但返回值是 [object Object] 我无法解析为字符串。
我怀疑在发送之前将编码参数解析为字符串可能会导致问题,如我的 IDE 中的 Node 和邮递员中我不使用 toString() 并且我对粘贴的 url 有正确的响应。但是在 Zappier 中,如果我使用相同的代码,我会得到响应 422“Unprocessable Entity”。只有在将 encodedParams 解析为字符串后,我才设法获得响应代码 200。我被卡住了。
我希望我的问题格式正确,这是我第一次使用 StackOverflow。谢谢!
【问题讨论】:
标签: javascript node.js http zapier pastebin