postmanlabs / newman

Newman is a command-line collection runner for Postman
https://www.postman.com
Apache License 2.0
6.88k stars 1.16k forks source link

Newman JSON reporter saves Response Body as String, not JSON format #1088

Closed rzizza closed 7 years ago

rzizza commented 7 years ago
  1. Newman Version: 3.5.2
  2. OS details : Windows 7
  3. I am calling Newman from Powershell
  4. This behavior has existed since I've used Newman
  5. Expected behavior: The Response Body saved in the JSON output file should be in JSON format, not a string, so it can be accessed (from Powershell, etc.)
  6. Command / script used to run Newman: Powershell

Steps to reproduce the problem:

  1. I ran my collection from Powershell using the following line:

newman run $collection -r 'cli,json' --no-color --disable-unicode --reporter-json-export $wwwhReportPath -d $wwwhInput -n $iterations | Tee-Object $consoleReportPath

  1. When the JSON report file is created, my Response Body looks like this:

image

I would really like to be able to use Powershell's ConvertFrom-Json and be able to access the individual key-value pairs of the response, but since the body is a string, I can't do that. Using ConvertTo-Json on this string only seems to convert the string to depth of 1 (and all the deeper levels of the JSON are still a string), despite using -depth 5 while converting to JSON.

Does anyone else have this issue, or a work around?

kunagpal commented 7 years ago

@rzizza The ConvertFrom-Json utility requires parsing depth to be specified as -Depth, notice the capitalized D. Feel free to reopen this issue if you're experiencing difficulties. 😄

rzizza commented 7 years ago

Thanks @kunagpal, but this does not solve the issue.

First of all, I assume you mean ConvertTo, not ConvertFrom, since ConvertFrom does not have a -Depth parameter. The issue is that the original JSON saved from the Postman/Newman results is not saving the Response Body as JSON.

To elaborate, after my Newman execution, the JSON report file from Postman is saved such that the response body looks like this:

image

I want to access the value in (JSON File).executions[1].response.body.results[0].geometry.location, so in Powershell, I have:

image

However, this result is [null]. If I instead do $wwwhResults[1].response.body, then I will get back the string body seen above. When attempting to convert $wwwhResults[1].response.body to JSON format and then back again to access the values, the value returned is still a single object containing the string body (the Depth is not working on this string, I'm guessing because all the \n's are causing the string to not properly be "in JSON format").

image

When I use Write-Host on the response body, it looks nice, because the "\n's" are formatting the string nicely. But the actual object of the response body is still a string.

image

I guess maybe a work-around would be to strip the extraneous characters and spaces from the response body, and reformat before trying to use ConvertTo-JSON.

I was just hoping that Newman could save the JSON Response Body in the proper JSON formatting, like how it is returned in the response from the Postman App.

Let me know if you have any questions.

kunagpal commented 7 years ago

In that case, a workaround would be to use Newman programmatically, like so:

var newman = require('newman'); // run npm install newman in the same folder as this script

newman.run({
    collection: process.env.collection, // equivalent to $collection
    reporters: ['json'],
    reporter: {
        json: {
            export: process.env.wwwhReportPath
        }
    },
    iterationData: process.env.wwwhInput,
    iterationCount: Number(process.env.iterations),
    noColor: true,
    disableUnicode: true
}, function (err, summary) {
    err && console.error(err);

    summary.run.executions.forEach(function (execution) {
        // execution.response.json() provides a JSON representation of the response body
    });
});

With the above code in a file (script.js, say), you could run your collection like so:

node script.js

Within the forEach block in the above snippet, you could process the JSON response bodies in any desired way.

kunagpal commented 7 years ago

Additionally, you'll also have to change $collection, etc in the provided sample, to $env:collection, so that newman can pick up those values correctly from the environment.

rzizza commented 7 years ago

Thank you, this looks like it might work for me. I will let you know when I get around to implementing it!

kunagpal commented 7 years ago

@rzizza Glad to hear that. I'm closing this issue for now, but feel free to get back 😄