简体   繁体   English

如何通过 Zapier 向 Pastebin API vie Code 发送 Post 请求并获取?

[英]How to send a Post request to the Pastebin API vie Code by Zapier and fetch?

I'm trying to formulate a post request to the Pastebin API in Code by Zapier using fetch.我正在尝试使用 fetch 在 Zapier 的代码中向 Pastebin API 制定发布请求。 After some trial and error, I managed to do it using URLencoded data, like this:经过一些试验和错误,我设法使用 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}

I get the response 200 but the data I receive is something I've never seen in my life.我收到响应 200,但我收到的数据是我一生中从未见过的。 It's something like this:是这样的:

 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

And then it continues with these rows of numbers for thousands of lines.然后它继续使用这些数字行数以千计的行。 I tried to use decodeURIcomponent but the returned value is [object Object] that I can't parse into a string.我尝试使用 decodeURIcomponent 但返回的值是 [object Object] 我无法解析为字符串。

I suspect maybe parsing encodedParams to string before sending can cause the proble as in Node in my IDE and in postman I don't use toString() and I have the correct response with the url to my paste.我怀疑在发送之前将编码参数解析为字符串可能会导致问题,如我的 IDE 和 postman 中的节点中我不使用 toString() 并且我对我的 Z572D4E421E5E6B71BC12Z 有正确的响应我的粘贴E5E6B71BC111D8。 But in Zappier if I use the same code I get the response 422 "Unprocessable Entity".但是在 Zappier 中,如果我使用相同的代码,我会得到响应 422“Unprocessable Entity”。 Only after parsing encodedParams to string did I manage to get a response code 200. I'm stuck.只有在将 encodedParams 解析为字符串后,我才设法获得响应代码 200。我被卡住了。

I hope my question is formatted ok, this is my first time on StackOverflow.我希望我的问题格式正确,这是我第一次使用 StackOverflow。 Thanks谢谢

This is actually working, you can see in your response you have a status of 200 , the problem is, you didn't extract the data out of the HTTP response.这实际上是有效的,您可以在响应中看到您的状态为200 ,问题是,您没有从 HTTP 响应中提取数据。

You need to make this change to see the actual data:您需要进行此更改才能查看实际数据:

const fetchResult = await fetch(url, options);
const data = await fetchResult.json();

output =  {output: data}

You could also replace .json() with .text() if you prefer.如果您愿意,也可以将.json()替换为.text()

I hope this helps我希望这有帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM