Content-type multipart/form-data vs application/json when submitting a form
Hello guys, sorry to disturb you all; consider the following code:
29 Replies
I didn't understand, what is the difference between these 2 type of content-type ? My server can handle application/json but not multipart/form-data.... like here:
So when I try to send the form without using JSON.stringify in the body, I obtain an error with invalid json format
But what I want to know, what is happening, I'm lost here, I know there are 2 content-type. When we use something like that in our server script
app.use(express.json());
What are we saying here? How our server is able to handle json files ?how the server is handling the json file, there's no need to worry about it. i mean it's the same question as how does the server listen to a certain port. indeed it is something you can learn if u want, but this is nothing to worry about for you project ot when u r making a server
in order to use json, the json that comes with the req needs some preprocessing. that preprocessing is done by express.json() middleware. now WHAT preprocessing it does, how it goes it is irrelevant
u can access the data from req.body i think
yep I see, thanks
also the multipart form thingy
form is a bit pain in the azz to work with
i think u need the multar library for it
search nodejs multar
it let's u handle form data + files
multer yes
just installed it
is it better to work with multipart or json ?
if u r going to send files to the server then multar
otherwise if just shrimple text data then i think either would work. but if u still have issues without multar then try with multar
ok, ty !
welcm
btw I have a project i did long ago
a backend project
u can check it out and see if u can kind of understand what's going on
not really sure how much it'll help u, but i was using most of the things u will be usign. like 90%
u can also probably get an idea on how to structure your backend
GitHub
GitHub - glutonium69/backend-practice
Contribute to glutonium69/backend-practice development by creating an account on GitHub.
oki, will have a look, do you have a github ?
ah ty !
welcome
it's actually a lot more complicated that that
this indicates the type of data you are sending to the server
multipart/form-data
sends the POST
content split by boundaries
the server does it's magic to parse the body
application/json
means you're sending json data, which the server may or may not understand
this is not`the same as a json filei JUST realised that i said json file and not json data after reading your msg o_O
i was actually meaning json data but my monki brain said file
but ya i can tell json file would be quite different and rather complicated than simple json data. sorry 😅😅
it is important to distinguish both, because a json file is sent over
multipart/form-data
and json data can be sent as a file or over application/json
i agree
and don't forget the weird
application/x-www-form-urlencoded
basically, what you would send in a GET
request url is sent in the body of the requesthmmm i see
oh json file and json data are 2 different things?
yes
like how xml data and an xml file are 2 different things
soap servers use xml data, but sending an xml file is foreign to those
imagine that you are expecting a voice call and i send you the graphic of the soundwaves over mail
it's still my voice, but one is understandable to you and the other one is absolute nonsense
yeah I see, so in the case of json file and json data, we can easily parse json data but not json file, right ?
thats not what i said
i said that the server expects json data, but you send it form data with a file in it, the server will reject the request
it cant do anything with that
yeah I see, it can't do anything about that because it didn't expect that data; this can change though if we use necessary libraries or methods ?
nope
if the server expects egg whites and all you give it is a pan with a boiled egg, what do you expect the server to do?
hmm nothing, returns an error
because it isn't something what it understands
exactly
so, if the server expects
application/json
and you send multipart/form-data
, how do you want the server to respond to that?yep it can't
but the thing is, I set up a small server for form submission... intially, I was getting an error because the server expect application/json while it was receiving multipart/form-data. Then, I needed to use a library called "multer" I forgot what it did but then it works
ive never heard of multer
and what the server accepts depends 300% of how it was configured
yep I see, thanks !!
you're welcome