How to get all decimal digits provided in a json response in Postman? - decimal

I Have an API that returns the following (example):
{"rate": 3.568640920671274015}
In the Tests space, I try to retrieve the rate value:
pm.test("Set variables", function () {
var jsonData = pm.response.json();
pm.environment.set('new-rate', jsonData.rate);
But as result, I get only:
3.568640920671274
It seems that Postman is truncating the result. Is there a way to avoid that?
PS: The value came from a decimal. (inside de API).

Doing something like this:
pm.environment.set('new-rate', jsonData.rate.toPrecision(18));
worked to me.

Related

NodeJS and Iconv - "ISO-8859-1" to "UTF-8"

I created a NodeJS application which should get some data from an external API-Server. That server provides its data only as 'Content-Type: text/plain;charset=ISO-8859-1'. I have got that information through the Header-Data of the server.
Now the problem for me is that special characters like 'ä', 'ö' or 'ü' are shown as �.
I tried to convert them with Iconv to UTF-8, but then I got these things '�'...
My question is, what am I doing wrong?
For testing I use Postman. These are the steps I do to test everything:
Use Postman to trigger my NodeJS application
The App requests data from the API-Server
API-Server sends Data to NodeJS App
My App prints out the raw response-data of the API, which already has those strange characters �
The App then tries to convert them with Iconv to UTF-8, where it shows me now this '�' characters
Another strange thing:
When I connect Postman directly to the API-Server, the special characters get shown as they have too without problems. Therefore i guess my application causes the problem but I cannot see where or why...
// Javascript Code:
try {
const response = await axios.get(
URL
{
params: params,
headers: headers
}
);
var iconv = new Iconv('ISO-8859-1', 'UTF-8');
var converted = await iconv.convert(response.data);
return converted.toString('UTF-8');
} catch (error) {
throw new Error(error);
}
So after some deeper research I came up with the solution to my problem.
The cause of all trouble seems to lie within the post-process of axios or something similar. It is the step close after data is received and convertet to text and shortly before the response is generated for my nodejs-application.
What I did was to define the "responseType" of the GET-method of axios as an "ArrayBuffer". Therefore an adjustment in axios was necessary like so:
var resArBuffer = await axios.get(
URL,
{
responseType: 'arraybuffer',
params: params,
headers: headers
}
);
Since JavaScript is awesome, the ArrayBuffer provides a toString() method itself to convert the data from ArrayBuffer to String by own definitions:
var response = resArBuffer.data.toString("latin1");
Another thing worth mentioning is the fact that I used "latin1" instead of "ISO-8859-1". Don't ask me why, some sources even recommended to use "cp1252" instead, but "latin1" workend for me here.
Unfortunately that was not enough yet since I needed the text in UTF-8 format. Using "toString('utf-8')" itself was the wrong way too since it would still print the "�"-Symbols. The workaround was simple. I used "Buffer.from(...)" to convert the "latin1" defined text into a "utf-8" text:
var text = Buffer.from(response, 'utf-8').toString();
Now I get the desired UTF-8 converted text I needed. I hope this thread helps anyone else outhere since thse informations hwere spread in many different threads for me.

How to get unencoded Query Parameter in Node.js

I am doing Google Oath2 Implementation. For a particular authorization_code, I was constantly getting invalid_grant. I checked the value and found that the query string value was encoded.
Here is an example:
const parser = require('url');
url ='http://example.com/test?param=4%2F12'
const q = parser.parse(url, true).query
console.log(q)
My output here is
{ param: '4/12' }
I want my output to be
{ param: '4%2F12' }
as the correct auth code is a string with value 4%2F12. How do I implement this?. There may be
many manual ways to do this. Anything that needs a minimalistic code effort would be appreciated. Thanks in advance!
Simple. Just encode again the param using encodeURIComponent.
Example:
console.log(encodeURIComponent("4/12")) // Output: 4%2F12

How do you read/query the response body in API V2 of dialogflow-fulfillment?

In the v1, the request and response were specifically defined and read via -
console.log(request.body);
var input = request.body.queryResult;
In the v2, the request and response, both are wrapped inside the 'app'. My declarations of app are as below -
const {dialogflow} = require('actions-on-google');
const app = dialogflow({clientId: 'projectId'});
I have tried using the following but understood that it's not exactly the right way -
console.log(conv.request.body); //Getting undefined in console
//OR
console.log(app.request.body); //Getting undefined in console
var input = conv.request.body.queryResult;
Do I need to specifically mention request and response anywhere similar to the WebhookClient({request, response}) in V1?
Thanks in advance
After million trial and error, I finally found it and it's terribly simple
console.log(conv.body);
var input = conv.body.queryResult.queryText;
I know this seems to already be resolved. But I noticed that you did a console.log() on conv.body. Assuming this conv variable is the JSON response object you received from Dialogflow, I'd recommend doing console.log(JSON.stringify(conv)), which, not surprisingly prints the full JSON object to string in the console. This has saved me much time while trying to figure out the many JSON formats.

Express get router not returning whole parameter

I've got the following code:
app.get('/torrent/', function (req, res) {
res.json(req.query.magnet);
});
So when I visit http://server.com/torrent/?magnet=<insertmagneturlhere> I would like the whole magnet URL to be in the response.
Here's an example of a whole magnet: magnet:?xt=urn:btih:550321C3982A023C474A61C37E3082D9EA1C12CC&dn=some+file+name+here&tr=udp%3A%2F%2Fopen.demonii.com%3A1337%2Fannounce
However I'm only getting this: "magnet:?xt=urn:btih:550321C3982A023C474A61C37E3082D9EA1C12CC"
What about the & escapes the response and stops it there and how can I get the rest of it?
That's a very strange looking uri, but the & is the separator between query arguments, so you actually have more than one. Try reading the next one wth:
req.queryquery.dn
And the one after that as
req.query.tr

Get headers from a request using Nodejs

I'd like to get the headers form a request (ex: status code, content-lenght, content-type...). My code :
options = {
method:'HEAD'
host:"123.30.xxx.xxx"
port:80
}
http.request(options,(res)->
res.send JSON.stringify(res.headers)
)
but this is not working
Please help me :(
Your JSON is not valid and it appears that you are not instantiating options as a variable prior to it's use. ev0lutions code resolves these issues as well as ending the request.
For info on how to create valid JSON check out this tutorial:
http://www.w3schools.com/json/
You need to call .end() on your http.request() object in order to make your request - see the docs:
With http.request() one must always call req.end() to signify that you're done with the request - even if there is no data being written to the request body.
For example:
var options = {
method:"HEAD",
host:"google.com",
port:80
};
var req = http.request(options,function(res) {
console.log(JSON.stringify(res.headers));
});
req.end();
Another issue in your code is that res doesn't have a .send() method - if you're referring to another res variable (for example, containing the code that you have posted) then your variables will be conflicting. If not, you should double check what you're trying to do here.

Resources