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

k6/http - payload of post method is not correct for nested objects #2369

Closed
alazurenko opened this issue Feb 3, 2022 · 2 comments
Closed
Labels
bug new-http issues that would require (or benefit from) a new HTTP API ux

Comments

@alazurenko
Copy link

alazurenko commented Feb 3, 2022

Brief summary

Dear Team,

I'm testing a Service that is accepting the configuration JSON as a POST payload to it to perform a certain operation.
The payload of the POST method is not properly encoded for the nested object.

k6 version

v0.36.0 ((devel), go1.17.6, darwin/amd64)

OS

masOS 10.15.7

Docker version and image (if applicable)

No response

Steps to reproduce the problem

Create a test

import http from "k6/http";
import { sleep } from "k6";

export const options = {
  stages: [{ duration: "10s", target: 1 }],
};

const data = {
  id: "id",
  details: {
    url: "http://service.com",
    path: "/api/products",
  },
};

export default function () {
  const res = http.post("my-service.com", data);
  sleep(1);
}

Expected behaviour

The payload to the tested service looks like this:

{
  id: "id",
  details: {
    url: "http://product-service.com",
    path: "/api/products",
  },
}

Actual behaviour

The payload on a service looks like this:

{
  details: 'map[path:/api/products url:http://product-service.com]',
  id: 'id',
}
@alazurenko alazurenko added the bug label Feb 3, 2022
@na--
Copy link
Member

na-- commented Feb 3, 2022

This is not really a bug because k6 doesn't serialize JS objects to JSON automatically, it serializes them to application/x-www-form-urlencoded or multipart/form-data. Only the map[...] bit is problematic and likely needs to be changed, though it's not clear it should be into JSON and not something else, e.g. what PHP does for nested objects: details[path]=/api/products&details[url]=.... There is no actual standard for encoding nested objects as HTTP body parameters, just different conventions from different programming languages or libraries 🤷‍♂️ This bug is tracked in #1185

#878 is the open issue about automatically marshaling JS objects to JSON request bodies in some cases. However, as explained in the issue comments and some of the linked discussions, it's unlikely we'll do it in the current version of the k6/http API because of too much ambiguity.

For now, you have to manually use JSON.stringify() for your JSON request bodies, like this:

export default function () {
  const res = http.post("my-service.com", JSON.stringify(data));
  sleep(1);
}

I'll close this issue since it's mostly a duplicate of #878, please share any comments you have on this there. This is something we'll try to design better in the next version of the HTTP API, e.g. by having a dedicated REST Client that marshals everything to JSON by default or something like that.

@alazurenko
Copy link
Author

@na-- Thank you for your reply it was helpful!
Have a nice day!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug new-http issues that would require (or benefit from) a new HTTP API ux
Projects
None yet
Development

No branches or pull requests

2 participants