var formData = {
name: 'TestDeck',
description: 'This is a test deck for my api',
private: false,
shareable: false,
ttsLanguages: [],
blacklistedSideIndices: [],
blacklistedQuestionTypes: [],
gradingModes: [],
imageAttribution: '.jpg',
imageFile: fs.readFile('retext.png', 'utf8')
}
function createDeck(connection) {
request.post({
url: '<url>',
formData: formData,
headers: {
'Content-Type': 'multipart/form-data'
},
json: true
}),
function(err, resp, body) {
}
}
I am getting the error: TypeError: First argument must be a string or Buffer.
I honestly have no idea why, need help.
var formData = {
name: 'TestDeck',
description: 'This is a test deck for my api',
private: false,
shareable: false,
ttsLanguages: [],
blacklistedSideIndices: [],
blacklistedQuestionTypes: [],
gradingModes: [],
imageAttribution: 'https://www.logogarden./wp-content/uploads/lg-index/Example-Logo-6.jpg',
imageFile: fs.readFile('retext.png', 'utf8')
}
function createDeck(connection) {
request.post({
url: '<url>',
formData: formData,
headers: {
'Content-Type': 'multipart/form-data'
},
json: true
}),
function(err, resp, body) {
}
}
I am getting the error: TypeError: First argument must be a string or Buffer.
I honestly have no idea why, need help.
Share Improve this question edited Sep 9, 2017 at 20:14 Andy 63.6k13 gold badges71 silver badges98 bronze badges asked Sep 9, 2017 at 19:52 SpearmintSpearmint 871 gold badge1 silver badge11 bronze badges 1-
fs.readFile
is an asynchronous method, soimageFile
most likely isn't what you think it is – adeneo Commented Sep 9, 2017 at 19:59
1 Answer
Reset to default 5There are several problems in the code.
You get
TypeError: First argument must be a string or Buffer
because you are trying to send boolean valuefalse
in form data -- HTML form does not support boolean value. In HTML, checked checkbox will send its value, while unchecked checkbox won't.To fix the issue, you can change
false
to'FALSE'
(string) and parse it in server side.The use of
fs.readFile('retext.png', 'utf8')
is incorrect. To attach file in the form, the right way is:imageFile: fs.createReadStream('retext.png')
.When
formData: formData
is used inrequest.post(...)
, theContent-Type
of the HTTP request would bemultipart/form-data
automatically, you don't need to defineContent-Type
header again.Moreover, it is incorrect to set
json: true
, which will makeContent-Type
asapplication/json
. This conflict will makerequest
module confused, and may cause problem in some JavaScript environment.The callback function
function(err, resp, body){...}
should be part ofrequest.post(...)
, maybe it is a typo.
In summary, the correct code would look like:
var formData = {
name: 'TestDeck',
description: 'This is a test deck for my api',
private: 'FALSE',
shareable: 'FALSE',
ttsLanguages: [],
blacklistedSideIndices: [],
blacklistedQuestionTypes: [],
gradingModes: [],
imageAttribution: 'https://www.logogarden./wp-content/uploads/lg-index/Example-Logo-6.jpg',
imageFile: fs.createReadStream('retext.png')
}
function createDeck(connection) {
request.post({
url: '<url>',
formData: formData
}, function(err, resp, body) {
})
}