Transferring a JSON have values of different types

31 views
Skip to first unread message

madana gopal

unread,
Mar 29, 2019, 12:28:45 AM3/29/19
to nodejs
Hi Team,

We are trying to setup a http client and server (using express). We have to send single post request with data having two parameters (param1 of type string and param2 of type base64 data. What is the way to send this?. Please share if we have snippet for this.

Thanks.

Regards,
Madan

Chaiwa Berian

unread,
Mar 30, 2019, 1:48:08 PM3/30/19
to nodejs
JSON can have data represented in about 5 primitive data types: String, Number, Array, Boolean, and Null. So if you are using express, and you have a javascript object that you would like to send to the client as json, the data fields(or object properties) will be cast to any of the respective data types. In this case of the base64 it will be sent as a String type. Below is the example:

JavaScript Object:
Let user = new User({
  username: 'some_username',
  id: some base64 encoded value or variable
});

To send it as JSON using express,  you would simply do the below(res being the request object of your express middle-ware):
res.json(user).send();
This way the payload sent to the client will be a JSON Object like below:
{
   "username": "some_username",
   "id": "YmFzZTY0IGRlY29kZXI"

I hope this helps and good luck! :)

lilsweetcaligula

unread,
Mar 30, 2019, 1:48:08 PM3/30/19
to nodejs
Hi Madan,

I believe the client should be to able to send both param1 and param2 as strings. The server can then decode param2, the base64-encoded string, back into binary, and save the file.

You can use Node.js http.request or https.request (for secure connections), or you could use the `request` or `axios` package.


On Friday, March 29, 2019 at 6:28:45 AM UTC+2, madana gopal wrote:

Chaiwa Berian

unread,
Mar 30, 2019, 1:48:08 PM3/30/19
to nod...@googlegroups.com
JSON can have data represented in about 5 primitive data types: String, Number, Array, Boolean, and Null. So if you are using express, and you have a javascript object that you would like to send to the client as json, the data fields(or object properties) will be cast to any of the respective data types. In this case of the base64 it will be sent as a String type. Below is the example:

JavaScript Object:
Let user = new User({
  username: 'some_username',
  id: some base64 encoded value or variable
});

To send it as JSON using express,  you would simply do the below(res being the request object of your express middle-ware):
res.json(user).send();
This way the payload sent to the client will be a JSON Object like below:
{
   "username": "some_username",
   "id": "YmFzZTY0IGRlY29kZXI"

I hope this helps and good luck! :)


--
Job board: http://jobs.nodejs.org/
New group rules: https://gist.github.com/othiym23/9886289#file-moderation-policy-md
Old group rules: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
---
You received this message because you are subscribed to the Google Groups "nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nodejs+un...@googlegroups.com.
To post to this group, send email to nod...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/nodejs/3212d1f8-008f-4963-905d-06799927e558%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages