How to get the JSON response from newman - node.js

I tried to run the end point which is running fine on postman and I just exported as collection and running it through newman on JENKINS CI.
Command:
newman run <POSTMAN_COLLECTION>.json -r json,cli
I'm getting the response.json file in the current directory as like below file:
I'm not able to see the response body inside the json file.
I'm googled but no luck. Is there anyway to get the response body for this postmand_collection? how can I achieve this?
I just want to get the response body as json file and I need to use it as request for other service.

You could create a mini project and use Newman as a library and run it using a script. That way you could use the node fs module to write the response out to a file.
const newman = require('newman'),
fs = require('fs');
newman.run({
collection: '<Your Collection>'
}).on('request', function (error, data) {
if (error) {
console.error(error);
}
else {
fs.writeFile(`response.json`, data.response.stream.toString(), function (error) {
if (error) {
console.error(error);
}
});
}
});
This script is using the Newman .on('request') event, that will extract that information. If you wanted all the response bodies, you may need to modify this slightly and maybe use the appendFileSync to capture all the responses from the requests in the collection.

Related

Node.js fs.writeFile save to json keeps looping [duplicate]

I have followed many solutions provided in the previous questions but mine is not working. The problem is in .json extension. Whenever I use filename.json, the app will crash with ERR_CONNECTION_RESET but successfully created an empty .json file. However, if I change the extension to filename.txt, the fs.writeFile will successfully create the filename.txt with the data inside and the app will work as expected. Did I miss any configuration here to create the JSON file?
Here is the example code I used.
var jsonData = '{"persons":[{"name":"John","city":"New York"},{"name":"Phil","city":"Ohio"}]}';
// parse json
var jsonObj = JSON.parse(jsonData);
console.log(jsonObj);
// stringify JSON Object
var jsonContent = JSON.stringify(jsonObj);
console.log(jsonContent);
fs.writeFile("./public/output.json", jsonContent, 'utf8', function(err) {
if (err) {
console.log("An error occured while writing JSON Object to File.");
return console.log(err);
}
console.log("JSON file has been saved.");
});
So, ERR_CONNECTION_RESET means that the connection was closed midway. My guess, as in the comments, would be that it's a reloading server.
Try using --ignore public/**/*.json and it should work.

Streaming a zip download from cloud functions

I have a firebase cloud function that uses express to streams a zip file of images to the client. When I test the cloud function locally it works fine. When I upload to firebase I get this error:
Error: Can't set headers after they are sent.
What could be causing this error? Memory limit?
export const zipFiles = async(name, params, response) => {
const zip = archiver('zip', {zlib: { level: 9 }});
const [files] = await storage.bucket(bucketName).getFiles({prefix:`${params.agent}/${params.id}/deliverables`});
if(files.length){
response.attachment(`${name}.zip`);
response.setHeader('Content-Type', 'application/zip');
response.setHeader('Access-Control-Allow-Origin', '*')
zip.pipe(output);
response.on('close', function() {
return output.send('OK').end(); // <--this is the line that fails
});
files.forEach((file, i) => {
const reader = storage.bucket(bucketName).file(file.name).createReadStream();
zip.append(reader, {name: `${name}-${i+1}.jpg`});
});
zip.finalize();
}else{
output.status(404).send('Not Found');
}
What Frank said in comments is true. You need to decide all your headers, including the HTTP status response, before you start sending any of the content body.
If you intend to express that you're sending a successful response, simply say output.status(200) in the same way that you did for your 404 error. Do that up front. When you're piping a response, you don't need to do anything to close the response in the end. When the pipe is done, the response will automatically be flushed and finalized. You're only supposed to call end() when you want to bail out early without sending a response at all.
Bear in mind that Cloud Functions only supports a maximum payload of 10MB (read more about limits), so if you're trying to zip up more than that total, it won't work. In fact, there is no "streaming" or chunked responses at all. The entire payload is being built in memory and transferred out as a unit.

node request module - sample request failing

I'm currently using node 7.6.0 and am trying the npm request module 2.80.0. The module is installed via package.json npm install. Following their simple example I immediately get: "Failed: Cannot set property 'domain' of undefined". Its a straight copy paste except for the require part.
var request = require('../node_modules/request/request');
request('http://www.google.com', function (error, response, body) {
console.log('error:', error); // Print the error if one occurred
console.log('statusCode:', response && response.statusCode); // Print the response status code if a response was received
console.log('body:', body); // Print the HTML for the Google homepage.
});
Am I missing something or is there other dependencies I'm not aware of?
I required it incorrectly. Should just be node_modules/request. Confusing since there is an actual request.js inside the request folder with a module exports.
// Exports
Request.prototype.toJSON = requestToJSON
module.exports = Request

CKAN resource_create API

Am trying to read from list of files placed in a location using NodeJS and create resources for those files in CKAN.
Am using CKAN API v3 (/api/3) to create the resource.
NodeJS library used for iterating through files in a location : filehound
CKAN API Used : /api/3/action/resource_create
Code to iterate over files (please don't run it; it's only a sample snippet) :
// file operations helper
const fileHound = require("filehound");
// filesystem
const fs = require("fs");
// to make http requests
const superagent = require("superagent");
var basePath = "/test-path", packageId = "8bf37d22-0c25-40c0-8faa-7de12ff927f5";
var files = fileHound.create()
.paths(basePath)
.find();
files.then(function (fileNames) {
if (Array.isArray(fileNames)){
for (var fileName of fileNames) {
superagent
.post("http://test-ckan-domain.com/api/3/action/resource_create")
.set("Authorization", "78d5d219-37de-41f7-9443-188bc564051e")
.field("package_id", packageId)
.field("name", fileName)
.field("format", "CSV")
.attach("upload", fs.createReadStream(fileName))
.end(function (err, res) {
if (!err) console.log(fileName ,"Status Code ", res.statusCode);
else console.log("Error ", err);
});
}
}
});
After iterating through the files and uploading them to CKAN by creating a new resource, CKAN responds back with "success: true" and "id" of the resource created with HTTP status code 200.
However, only a few resource(s) are created and few are not. Am using CKAN's frontend to verify if the resource was created under a package/dataset (As the response from the Resource Creation API is successful).
Is it a known issue with CKAN? or am i going wrong in my code?
You've probably solved this by now, but given that some packages are just missing but you only get 200 success messages, I'm wondering if your for loop just isn't working correctly. Be careful with the syntax; I think declaring var filename inside the loop like that might be wrong.
https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Statements/for...of

Parse Cloud Code File Request Fail

I'm having some issues trying to request an image file from Cloud Clode in Parse.
This is my Parse Cloud Code:
Parse.Cloud.define("datata", function(request, response) {
//var message = request.params.message;
var file = request.params.file;
//console.log(file);
var base64 = file.toString("base64");
var data = new Parse.File("test.jpg", {
base64: base64
});
data.save().then(function() {
// The file has been saved to Parse.
console.log("WIN");
}, function(error) {
console.log("LOSE");
// The file either could not be read, or could not be saved to Parse.
});
});
The problem is when I try to post the file I got this as an answer from the server:
{"code":107,"error":"invalid utf-8 string was provided"}
I'm trying to create custom endpoints for some custom hooks, that why I'm working with Cloud Code.
Anyone have any idea about how can I create and endpoint in Parse Cloud Code for requesting and creating files?
Thanks in advance.
What JSON response did you get when you POST'd the file?
You need to use the "url" value in order to GET the file.
{"__type":"File","name":"e580f231-90ba-4d24-934c-7f9e7c8652d6-picf1","url":"http://files.parse.com/1315e4d8-f302-4337-adbe-d8650ab5c312/e580f231-90ba-4d24-934c-7f9e7c8652d6-picf1"}
So, in the example above which is very similar to the response when a file type is POST'd, you would use the value of the "url" tag in a http/GET.

Resources