Based on #1184, the following script:
import http from "k6/http";
export default function () {
let payload = {
data: "something",
another: null ,
};
let resp = http.post("https://httpbin.org/post", payload);
console.log(resp.body);
}
Will produce:
{
"args": {},
"data": "",
"files": {},
"form": {
"another": "<nil>",
"data": "something"
},
"headers": {
"Content-Length": "32",
"Content-Type": "application/x-www-form-urlencoded",
"Host": "httpbin.org",
"User-Agent": "k6/0.26.0-dev (https://k6.io/)"
},
"json": null,
"origin": "46.233.49.37, 46.233.49.37",
"url": "https://httpbin.org/post"
}
As can be seen by httpbin's response, k6 automagically encodes the object as x-www-form-urlencoded, which is due to this lines https://github.com/loadimpact/k6/blob/1d6fb7f668aa089b374ee8ca35de2a34d77d29ce/js/modules/k6/http/request.go#L148-L151
A quick proposal is to check for null and return "" but I am not certain so we need to read some RFCs, probably or something.
Do i get that right, that we actually want a json encoding on the value here? (As the json should be null and not the empty string?)
I would use the json.Encoding package here to get the right values? E.g. https://goplay.space/#lnqhSFq-0gi
@simonfrey, no, we don't want JSON encoding. To preserve backwards compatibility, k6 should encode JS objects passed as the POST body in an application/x-www-form-urlencoded format, or multipart/form-data, if a file is being sent. This issue is for fixing the strangeness that null would be sent as the string <nil>.
To have a JSON encoded body, you can manually use JSON.stringify(data) in the script, for now. https://github.com/loadimpact/k6/issues/878 was the old issue about automatically supporting JSON bodies in a more user-friendly way, but it will be superseded by an upcoming issue, since we have some plans for implementing a new and better HTTP API in the coming months (issue to be written soonâ„¢).
Most helpful comment
@simonfrey, no, we don't want JSON encoding. To preserve backwards compatibility, k6 should encode JS objects passed as the POST body in an
application/x-www-form-urlencodedformat, ormultipart/form-data, if a file is being sent. This issue is for fixing the strangeness thatnullwould be sent as the string<nil>.To have a JSON encoded body, you can manually use
JSON.stringify(data)in the script, for now. https://github.com/loadimpact/k6/issues/878 was the old issue about automatically supporting JSON bodies in a more user-friendly way, but it will be superseded by an upcoming issue, since we have some plans for implementing a new and better HTTP API in the coming months (issue to be written soonâ„¢).