Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automatically urlencoded request bodies encode null as <nil> and objects as map[...] #1185

Open
mstoykov opened this issue Oct 4, 2019 · 3 comments
Labels
bug evaluation needed proposal needs to be validated or tested before fully implementing it in k6 help wanted new-http issues that would require (or benefit from) a new HTTP API

Comments

@mstoykov
Copy link
Contributor

mstoykov commented Oct 4, 2019

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.

@simonfrey
Copy link

simonfrey commented Oct 5, 2019

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

@na--
Copy link
Member

na-- commented Oct 7, 2019

@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. #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™).

@na-- na-- changed the title The magic urlencode on bodies encodes null as "<nil>" Automatically urlencoded request bodies encode null as "<nil>" Oct 7, 2019
@na-- na-- added this to the v0.27.0 milestone Oct 10, 2019
@na-- na-- added evaluation needed proposal needs to be validated or tested before fully implementing it in k6 and removed hacktoberfest labels Mar 9, 2020
@na-- na-- removed this from the v0.27.0 milestone Mar 9, 2020
@na-- na-- added the new-http issues that would require (or benefit from) a new HTTP API label Oct 18, 2021
@na--
Copy link
Member

na-- commented Feb 3, 2022

Another very similar issue is that nested objects are encoded as map[...], see #2369 for an example

@na-- na-- changed the title Automatically urlencoded request bodies encode null as "<nil>" Automatically urlencoded request bodies encode null as <nil> and objects as map[...] Feb 3, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug evaluation needed proposal needs to be validated or tested before fully implementing it in k6 help wanted new-http issues that would require (or benefit from) a new HTTP API
Projects
None yet
Development

No branches or pull requests

3 participants