[critical] POST requests with JSON data using a {{variable}} set to undefined or null fails with error: Cannot read property 'toString' of undefined
See original GitHub issueNewman exists when a variable is being used in the request body, but the variable was set to undefined or null, as it was not available in the previous request where it was being set.
Rather than passing the variable as it is and the tests failing, newman exits with error and no reports are created.
Error shown by newman: Cannot read property βtoStringβ of undefined
- Newman Version (can be found via
newman -v
): 3.9.2 - OS details (type, version, and architecture): Windows 7 Pro, x64
- Are you using Newman as a library, or via the CLI? CLI
- Did you encounter this recently, or has this bug always been there: Probably after 3.8.2
- Expected behaviour: Any undefined or null variables should be resolved to undefined/null and sent accross
- Command / script used to run Newman:
- Sample collection, and auxiliary files (minus the sensitive details): Pasted at the end
- Screenshots (if applicable):
Steps to reproduce the problem:
- Create a POST Request with JSON body
- In Pre-requisite or a request before, Set the variable to undefined. For example:
pm.environment.set("version", undefined);
- In JSON body use a variable, for example :
{"version":"{{version}}"}
- Run the collection using newman
Newman CLI shows following output: Temp
β After comparisons GET https://postman-echo.com/time/after?timestamp={{start}}&target=2017-10-10 [400 Bad Request, 408B, 1622ms]
β POST Raw Text
βββββββββββββββββββββββββββ¬βββββββββββ¬βββββββββββ β β executed β failed β βββββββββββββββββββββββββββΌβββββββββββΌβββββββββββ€ β iterations β 0 β 0 β βββββββββββββββββββββββββββΌβββββββββββΌβββββββββββ€ β requests β 1 β 0 β βββββββββββββββββββββββββββΌβββββββββββΌβββββββββββ€ β test-scripts β 1 β 0 β βββββββββββββββββββββββββββΌβββββββββββΌβββββββββββ€ β prerequest-scripts β 1 β 0 β βββββββββββββββββββββββββββΌβββββββββββΌβββββββββββ€ β assertions β 0 β 0 β βββββββββββββββββββββββββββ΄βββββββββββ΄βββββββββββ€ β total run duration: 0ms β βββββββββββββββββββββββββββββββββββββββββββββββββ€ β total data received: 0B (approx) β βββββββββββββββββββββββββββββββββββββββββββββββββ€ β average response time: 1622ms β βββββββββββββββββββββββββββββββββββββββββββββββββ Cannot read property βtoStringβ of undefined
Postman Collection Runner gives following error stack: message : βCannot read property βtoStringβ of undefinedβ name : βTypeErrorβ stack : βTypeError: Cannot read property βtoStringβ of undefinedβ΅ at PostmanVariable.toString (C:\Users\aadm221\AppData\Local\Postman\app-5.5.0\resources\app.asar\node_modules\postman-collection\lib\collection\variable.js:108:34)β΅ at C:\Users\aadm221\AppData\Local\Postman\app-5.5.0\resources\app.asar\node_modules\postman-collection\lib\superstring\index.js:221:53β΅ at C:\Users\aadm221\AppData\Local\Postman\app-5.5.0\resources\app.asar\node_modules\postman-collection\lib\superstring\index.js:47:23β΅ at RegExp.[Symbol.replace] (<anonymous>)β΅ at String.replace (native)β΅ at SuperString.replace (C:\Users\aadm221\AppData\Local\Postman\app-5.5.0\resources\app.asar\node_modules\postman-collection\lib\superstring\index.js:45:34)β΅ at Substitutor.parse (C:\Users\aadm221\AppData\Local\Postman\app-5.5.0\resources\app.asar\node_modules\postman-collection\lib\superstring\index.js:132:27)β΅ at Function.replaceSubstitutions (C:\Users\aadm221\AppData\Local\Postman\app-5.5.0\resources\app.asar\node_modules\postman-collection\lib\collection\property.js:222:69)β΅ at Function.<anonymous> (C:\Users\aadm221\AppData\Local\Postman\app-5.5.0\resources\app.asar\node_modules\postman-collection\lib\collection\property.js:253:25)β΅ at C:\Users\aadm221\AppData\Local\Postman\app-5.5.0\resources\app.asar\node_modules\postman-collection\lib\collection\property.js:247:40β΅ at C:\Users\aadm221\AppData\Local\Postman\app-5.5.0\resources\app.asar\node_modules\postman-collection\node_modules\lodash\lodash.js:4944:15β΅ at baseForOwn (C:\Users\aadm221\AppData\Local\Postman\app-5.5.0\resources\app.asar\node_modules\postman-collection\node_modules\lodash\lodash.js:3001:24)β΅ at Function.forOwn (C:\Users\aadm221\AppData\Local\Postman\app-5.5.0\resources\app.asar\node_modules\postman-collection\node_modules\lodash\lodash.js:13002:24)β΅ at Function.<anonymous> (C:\Users\aadm221\AppData\Local\Postman\app-5.5.0\resources\app.asar\node_modules\postman-collection\lib\collection\property.js:246:19)β΅ at C:\Users\aadm221\AppData\Local\Postman\app-5.5.0\resources\app.asar\node_modules\postman-collection\lib\collection\property.js:247:40β΅ at C:\Users\aadm221\AppData\Local\Postman\app-5.5.0\resources\app.asar\node_modules\postman-collection\node_modules\lodash\lodash.js:4944:15β΅ at baseForOwn (C:\Users\aadm221\AppData\Local\Postman\app-5.5.0\resources\app.asar\node_modules\postman-collection\node_modules\lodash\lodash.js:3001:24)β
Sample Collection:
{
"info": {
"name": "Temp",
"_postman_id": "734b99af-72b1-a510-dc4c-10214c91aeee",
"description": "",
"schema": "https://schema.getpostman.com/json/collection/v2.0.0/collection.json"
},
"item": [
{
"name": "After comparisons",
"event": [
{
"listen": "test",
"script": {
"id": "b8d9c216-89af-4c77-8ff6-7066d5e8fd77",
"type": "text/javascript",
"exec": [
"pm.environment.set(\"version\", undefined);"
]
}
}
],
"request": {
"method": "GET",
"header": [],
"body": {},
"url": {
"raw": "https://postman-echo.com/time/after?timestamp={{start}}&target=2017-10-10",
"protocol": "https",
"host": [
"postman-echo",
"com"
],
"path": [
"time",
"after"
],
"query": [
{
"key": "timestamp",
"value": "{{start}}",
"equals": true
},
{
"key": "target",
"value": "2017-10-10",
"equals": true
}
]
},
"description": "A simple `GET` request to `/time/after` to check if the provided timestamps is after a comparison `target` (query parameter).\n\nThis endpoint accepts `timestamp`, `locale`, `format`, `strict`, and `target` query parameters to construct the date time instance to check against.\n\nResponses are provided in JSON format, with a `after` key to indicate the result. The response code is `200` for valid query parameters, and `400` otherwise.\n\n```\n{\n after: true/false\n}\n```"
},
"response": []
},
{
"name": "POST Raw Text",
"event": [
{
"listen": "test",
"script": {
"id": "eca79d5c-0416-4fc6-b4b6-eafe6f371f05",
"type": "text/javascript",
"exec": [
"var responseJSON;",
"",
"try { ",
" responseJSON = JSON.parse(responseBody); ",
" tests['response is valid JSON'] = true;",
"}",
"catch (e) { ",
" responseJSON = {}; ",
" tests['response is valid JSON'] = false;",
"}",
"",
"",
"tests['response has post data'] = _.has(responseJSON, 'data');",
"tests['response matches the data posted'] = (responseJSON.data && responseJSON.data.length === 256);",
"",
"tests[\"content-type equals text/plain\"] = responseJSON && responseJSON.headers && (responseJSON.headers[\"content-type\"] === 'text/plain');"
]
}
},
{
"listen": "prerequest",
"script": {
"id": "c5d968db-e436-46e3-b4fe-cda9d6ebab89",
"type": "text/javascript",
"exec": [
""
]
}
}
],
"request": {
"method": "POST",
"header": [
{
"key": "Content-Type",
"value": "application/json"
}
],
"body": {
"mode": "raw",
"raw": "{\n\t\"name\":\"postman\",\n\t\"value\":\"{{version}}\"\n}"
},
"url": "https://postman-echo.com/post",
"description": "The HTTP `POST` request method is meant to transfer data to a server \n(and elicit a response). What data is returned depends on the implementation\nof the server.\n\nA `POST` request can pass parameters to the server using \"Query String \nParameters\", as well as the Request Body. For example, in the following request,\n\n> POST /hi/there?hand=wave\n>\n> <request-body>\n\nThe parameter \"hand\" has the value \"wave\". The request body can be in multiple\nformats. These formats are defined by the MIME type of the request. The MIME \nType can be set using the ``Content-Type`` HTTP header. The most commonly used \nMIME types are:\n\n* `multipart/form-data`\n* `application/x-www-form-urlencoded`\n* `application/json`\n\nThis endpoint echoes the HTTP headers, request parameters, the contents of\nthe request body and the complete URI requested."
},
"response": []
}
]
}
Issue Analytics
- State:
- Created 6 years ago
- Comments:5 (5 by maintainers)
Not intentionally sending undefined.
There are a lot of cases, where some data is set from the response of a previous request , but in case no data is received for that field, it gets set to undefined/null. Donβt want to put all kinds of null checks in these requests as they are used for fetching data.
So the later request which is using this variable would eventually fail, since the response would give error.
But I do not expect newman to completely exit with no reports and without finishing with all the tests.
Also, I am sure this was working in 3.8.2
@aksbenz This has been fixed with Newman v3.9.3. π