Nodejs http server json parse - node.js

why the parameter data different in console and website
i need use JSON.stringify or JSON.parse in res.end?

The JSON.stringify() method converts a JavaScript object or value to a JSON string, so add this to your res.end()
res.end(JSON.stringify(data));

I think this code will help you.
res.end(data.toString())
I'll explain the reason why it is different in console and website.
In console, the data is type of buffer, so it can probably output data in byte format.
But in the web browser, the data is parsed in JSON string, so it'll be converted to string and correct result comes out.

Related

What is this structure coming from a fetch that returns a octet-stream?

I'm tring to retrieve a list of files from an API using axios, in Node.js. What I get is what I assume is an ArrayBuffer string. I'm using the following code:
axios.get('http://api.com/filesList').then(response => {
console.log(response.data);
});
This is what I get (changed numbers and letters a bit to hide potential priv info):
$97cceb28↕$sad28sdj-askd-3294-3jh9→--Account_123456789 �����0*$97cceb282¶2022-11-30.csv"�☺
$97cceb28↕$sad28sdj-askd-3294-3jh9→--Account_123456789 ���0*$97cceb282¶2022-11-26.csv"�☺
$97cceb28↕$sad28sdj-askd-3294-3jh9→--Account_123456789 �໑�0*$97cceb282¶2022-11-19.csv"�☺
I know for a fact that I'm supposed to receive certain information for each file of this list, at least which account is associated to them and their name. I did a little digging and added responseType: 'arraybuffer' to the function and then it output a <Buffer 22 be 01...> kind of response. I then tried a method to convert it to a string but I end up getting the result I had without setting the response type.
So, what kind of string data is this and how do I convert it to an object/JSON? Is there a way to decode this structure?
Thank you all in advance.

Obtain data from JSON object with NODE JS

I have a response from a webservice like this:
[{"record_id":"63","date":"2021-04-12","acept":"1","name":"John","document":"1","passport":"","phone":"999999999","sign":"[document]","activity":"2"}]
There is a var called response that stores that response.
How do I get the "name" and the "phone" from this?
I tried to do JSON.stringify(response) in order to get somehow the info but I don't know what to do next.
Is the response a JSON or just a String??
Should I do JSON.stringify or JSON.parse to be able to work with this?
Thanks a lot
To check to see whether this is a JSON object or string, run console.log(typeof response). If it logs object, then this is already a JSON object! You don't have to do anything, and can get attributes out of it like any other object. (For instance, to get the name attribute, you can run response[0]["name"].) If it logs string, then you'll have to run JSON.parse(response) and save that to a variable to parse the string and turn it into an object.
After parse the response, you can access to name and phone like this:
response[0]["name"]
response[0]["phone"]

NetSuite SuiteScript 2.0 How to parse content Text in suitescript

i am trying to update a vendor record status field using suitescript 2.0,passing the body in postman tool and is working fine for content JSON but the problem is when i try content as Text its getting error don't know how to read body value in suitescript 2.0.
input body from postman
sample code is
function doPut(context)
{
var obj=JSON.stringify(context.ids);-----here is the error context is empty
// tried JSON.parse also getting undefined
log.debug('str: '+obj);
return obj;
}
If you could provide the exact error message that would be helpful.
But in the mean time a few things that you should verify in your script, if you do not pass application/JSON in header, and your data is object, you need to explicitly parse it into JSON(i.e use JSON.parse() on the request-body), and your response type too should be in the same format i.e your response type should match content-type in the request.
looks like you may have the wrong Content-type. Should be Application-json. If not try Json.parse on the body if you're using text/plain. First step is to always log the context to console or run Object.keys(context) to see what's there. Also make sure doPut is exported as a function

Saving socket connection to JSOn

I am using socket.io websockets in nodejs. I am trying to stringify the socket object to be able to save it into my database. Here is what I am doing:
socket.on('open-room', function(arg, callback) {
var socketStr = JSON.stringify(socket);
}
But I am getting the following error:
TypeError: Converting circular structure to JSON
If you're looking for a general solution on how to convert an object into a JSON string without encountering a circular structure error (usually, you would do this for logging or debugging), check out the S.O. answer Converting Circular Structure to JSON. If it's not important exactly what the output format is, you can use the built in util.inspect(socket).
If you're doing this for any reason other than logging, be aware that a socket.io websocket can't be serialized/deserialized (you won't be able to recreate a working websocket using the database record).
You might have better luck crafting a more specific JSON object containing only the keys you actually care about, and storing that in the database, rather than attempting to stringify the entire object.

Handling UTF8 characters in express route parameters

I'm having an issue with a NodeJS REST api created using express.
I have two calls, a get and a post set up like this:
router.get('/:id', (request, response) => {
console.log(request.params.id);
});
router.post('/:id', (request, response) => {
console.log(request.params.id);
});
now, I want the ID to be able to contain special characters (UTF8).
The problem is, when I use postman to test the requests, it looks like they are encoded very differently:
GET http://localhost:3000/api/â outputs â
POST http://localhost:3000/api/â outputs â
Does anyone have any idea what I am missing here?
I must mention that the post call also contains a file upload so the content type will be multipart/form-data
You should encode your URL on the client and decode it on the server. See the following articles:
What is the proper way to URL encode Unicode characters?
Can urls have UTF-8 characters?
Which characters make a URL invalid?
For JavaScript, encodeURI may come in handy.
It looks like postman does UTF-8 encoding but NOT proper url encoding. Consequently, what you type in the request url box translates to something different than what would happen if you typed that url in a browser.
I'm requesting: GET localhost/ä but it encodes it on the wire as localhost/ä
(This is now an invalid URL because it contains non ascii characters)
But when I type localhost/ä in to google chrome, it correctly encodes the request as localhost/%C3%A4
So you could try manually url encoding your request to http://localhost:3000/api/%C3%A2
In my opinion this is a bug (perhaps a regression). I am using the latest version of PostMan v7.11.0 on MacOS.
Does anyone have any idea what I am missing here?
yeah, it doesn't output â, it outputs â, but whatever you're checking the result with, think you're reading something else (iso-8859-1 maybe?), not UTF-8, and renders it as â
Most likely, you're viewing the result in a web browser, and the web server is sending the wrong Content-Type header. try doing header("Content-type: text/plain;charset=utf-8"); or header("Content-type: text/html;charset=utf-8"); , then your browser should render your â correctly.

Resources