-
Notifications
You must be signed in to change notification settings - Fork 258
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Remove request library #275
base: master
Are you sure you want to change the base?
Changes from 8 commits
3f6f131
c754134
d59343f
35ebed5
3250b10
32968ba
dca99b9
5b6551e
1a98bec
fff6024
2d35226
443621e
8572f74
9d8fc07
0f10de9
108d01a
aefbf19
66bf91a
cfafe62
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
'use strict'; | ||
|
||
const http = require('https'); | ||
|
||
const post = (url, data, cb) => { | ||
const options = { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
'Content-Length': Buffer.byteLength(data), | ||
}, | ||
}; | ||
|
||
let errored = false; | ||
|
||
const req = http.request(url, options, (res) => { | ||
res.setEncoding('utf8'); | ||
|
||
const { statusCode } = res; | ||
|
||
let body = ''; | ||
res.on('data', (chunk) => { | ||
body += chunk.toString(); | ||
}); | ||
|
||
res.on('end', () => { | ||
if (!errored) { | ||
cb(null, { statusCode }, body); | ||
} | ||
}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How i would have written it: let body = ''
for await (const chunk of res) {
body += chunk
}
cb(null, { statusCode }, body) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. would also take the opportunity to use Promise instead |
||
}); | ||
|
||
req.on('error', (e) => { | ||
errored = true; | ||
cb(e); | ||
}); | ||
|
||
// Write data to request body | ||
req.write(data); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Previously, the request was POST-ing as a urlencoded form, but here, you're just posting the JSON data raw without any prefix. So, eg, if the object was That's almost certainly why it's failing. Try something like this: req.write(`json=${encodeURIComponent(data)}`) |
||
req.end(); | ||
}; | ||
|
||
module.exports = { | ||
post, | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is brittle, because you'll get mochibake if any character boundaries get split. I'd recommend doing it this way instead:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thank you! i read about this years ago and forgot again, now i will remember it as the pattern to use.