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

771 views Asked by At

I'm trying to formulate a post request to the Pastebin API in Code by Zapier using fetch. After some trial and error, I managed to do it using URLencoded data, like this:

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. 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.

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. But in Zappier if I use the same code I get the response 422 "Unprocessable Entity". Only after parsing encodedParams to string did I manage to get a response code 200. I'm stuck.

I hope my question is formatted ok, this is my first time on StackOverflow. Thanks!

1

There are 1 answers

0
Tom Dickman On

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.

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.

I hope this helps!