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

Sending null values in post request #1184

Closed
Lex-talionis opened this issue Oct 4, 2019 · 6 comments
Closed

Sending null values in post request #1184

Lex-talionis opened this issue Oct 4, 2019 · 6 comments

Comments

@Lex-talionis
Copy link

Lex-talionis commented Oct 4, 2019

Hi, when trying to sending a null value to a post endpoint, k6 converts null into '<nil>'. is there a way to fix this in my end?

@na--
Copy link
Member

na-- commented Oct 4, 2019

Can you post your code and tell us which k6 version you are using, since this seems like a bug, but I can't reproduce it. This code:

import http from "k6/http";

export default function () {
    let resp = http.post("https://httpbin.org/post", null);
    console.log(resp.body);
}

produces

{
  "args": {}, 
  "data": "", 
  "files": {}, 
  "form": {}, 
  "headers": {
    "Content-Length": "0", 
    "Host": "httpbin.org", 
    "User-Agent": "k6/0.25.1 (https://k6.io/)"
  }, 
  "json": null, 
  "url": "https://httpbin.org/post"
} 

which seems normal

@Lex-talionis
Copy link
Author

Hi @na-- thanks for your quick response. Im sorry my last post wasnt clear at all

           let url = httpParams.url + "/api/v1/login";
           let payload = {
               username: "myusername",
               password: "mypassword",
               company_id: null ,
               vendor_id : null ,
               ldap_id : null ,
           };
           let params = {
               "Content-Type": "application/json"
           };
           let res = http.post(url, payload, params);

my response i get within the node api is

{
  company_id: '<nil>',
  ldap_id: '<nil>',
  password: 'mypassword',
  username: 'mypassword',
  vendor_id: '<nil>' 
}

@mstoykov
Copy link
Collaborator

mstoykov commented Oct 4, 2019

This is duplicate of #878, so I am closing this.

For a quick fix you can useJSON.stringify(payload)(the issue above explains what is happening but as :

import http from "k6/http";

export default function () {
    let payload = {
        username: "myusername",
        password: "mypassword",
        company_id: null ,
        vendor_id : null ,
        ldap_id : null ,
    };
    let params = {
        "Content-Type": "application/json"
    };
    let resp = http.post("https://httpbin.org/post", JSON.stringify(payload), params);
    console.log(resp.body);
}

will get you:

 {
  "args": {},
  "data": "{\"username\":\"myusername\",\"password\":\"mypassword\",\"company_id\":null,\"vendor_id\":null,\"ldap_id\":null}",
  "files": {},
  "form": {},
  "headers": {
    "Content-Length": "99",
    "Host": "httpbin.org",
    "User-Agent": "k6/0.26.0-dev (https://k6.io/)"
  },
  "json": {
    "company_id": null,
    "ldap_id": null,
    "password": "mypassword",
    "username": "myusername",
    "vendor_id": null
  },
  "origin": "46.233.49.37, 46.233.49.37",
  "url": "https://httpbin.org/post"
}

I have also opened new issue to fix the problem when urlencoding: #1185

@mstoykov mstoykov closed this as completed Oct 4, 2019
@Lex-talionis
Copy link
Author

@mstoykov thanks for your response i did have a check on existing issues and couldn't find a similar one, obviously i didn't look hard enough.

@na--
Copy link
Member

na-- commented Oct 7, 2019

Don't worry @Lex-talionis, the issue wasn't easy to find since it stated the same problem in a different way... We just knew it because we've hit it before, and even then, your issue told us something that we didn't know before about the way null values are encoded, which we'd like to fix now, thus the new issue. The other reason we knew about the old issue is because we're currently investigating how to add a new and better HTTP API in k6 that would both be more flexible, and also solve issues like this that such confusion and frustration.

@aniketRaj-goPlus
Copy link

aniketRaj-goPlus commented Mar 14, 2023

Use blank string instead of null in your json format like this:

import http from "k6/http";

export default function () {
    let payload = {
        username: "myusername",
        password: "mypassword",
        company_id: "" ,
        vendor_id : "" ,
        ldap_id : "" ,
    };}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants