POST request Json Body

Hi, I’m experiencing some issues when running this code, my intention is to make sure a I get back a 200 status code in response, but I don’t get it. I have no idea why…

import http from 'k6/http';

import { check} from 'k6';

export let options = {

      vus: 1,

      duration: '3s'

    }

const url = 'http://localhost:8083/service-now/writeInSnow';

export default function () {

  let data = { "user_sys_id": "904301771b0fd090243b20ad3b4bcb06",

    "tech_sys_id": "1e9f2829e5e33100ea07c36ce50dc80a",

    "incident_sys_id": "e1bc611e1b396410dd077669cd4bcb43",

    "scheduledStart": "2021-02-22 16:00:00",

    "scheduledEnd": "2021-02-22 16:45:00",

    "u_state": 17,

    "u_operation": "submit",

    "comment": "RDV pris / Appointment taken"

  };

  let res = http.post(url, JSON.stringify(data),{ headers: { 'Content-Type': 'application/json' } });

// check code status                      

check(res, { "status is 200": r => r.status === 200}) 

}

request and body
k6-com2

headers request

headers response

HI @Isaac,

I recommend you console.log-ing the code or even the whole response:

console.log(JSON.stringify(res, null, "  "));

Should “pretty” print it.

This should give you some leads, and if not you can post it here so we can try to figure it out together.

Thanks, I followed your advice and came across something weird. I’ve changed the code (this is actually the one that I need to execute). There are just 2 requests, first one creates a service and second one cancels the service created. Cannot create a service that already exists and either can cancel a service that doesn’t exit.Therefore I execute the requests in sequence to avoid (code status 500), but when executing the second request ‘cancel’ I get an error ‘code status 415’ (check logs). Curiously when executing any of the requests independently everything is fine, I just get the problem when running both at the same time as a Group.

SCRIPT

import http from 'k6/http';
import { check, sleep ,group} from 'k6';

let options = {
  vus: 1,
  duration: '7s',
};
const SLEEP_DURATION = 3;

export default function () {
    const url = 'http://localhost:8083/service-now/writeInSnow';
    let headers = { 'Content-Type': 'application/json' };
    let data_create = { "user_sys_id": "904301771b0fd090243b20ad3b4bcb06",
    "tech_sys_id": "1e9f2829e5e33100ea07c36ce50dc80a",
    "incident_sys_id": "e1bc611e1b396410dd077669cd4bcb43",
    "scheduledStart": "2021-02-22 16:00:00",
    "scheduledEnd": "2021-02-22 16:45:00",
    "u_state": 17,
    "u_operation": "submit",
                  "comment": "RDV pris / Appointment taken"};
    let data_cancel = { "user_sys_id": "904301771b0fd090243b20ad3b4bcb06",
    "tech_sys_id": "1e9f2829e5e33100ea07c36ce50dc80a",
    "incident_sys_id": "e1bc611e1b396410dd077669cd4bcb43",
    "scheduledStart": "2021-02-22 16:00:00",
    "scheduledEnd": "2021-02-22 16:45:00",
    "u_state": 4,
    "u_operation": "close",
                  "comment": "RDV pris / Appointment taken"};

  group('secuence',function () {
    // Launching create  
    let res_create = http.request('POST', url, JSON.stringify(data_create), {
        headers: headers,
      });

        console.log(JSON.stringify(res_create, null, "  "));
        check(res_create, {
        'is status 200 create': (r) => r.status === 200,"status is 500 create": r => r.status === 500,});
        sleep(SLEEP_DURATION);

        headers = { 'Content-Type': 'application/x-www-form-urlencoded' };
        res_create = http.request('POST', url, data_create, { headers: headers });

    // Launching cancel  
    let res_cancel = http.request('POST', url, JSON.stringify(data_cancel), {
        headers: headers,
      });
        console.log(JSON.stringify(res_cancel, null, "  "));
        check(res_cancel, {
        'is status 200 cancel': (r) => r.status === 200,"status is 500 cancel": r => r.status === 500,});
        sleep(SLEEP_DURATION);

        headers = { 'Content-Type': 'application/x-www-form-urlencoded' };
        res_cancel = http.request('POST', url, data_cancel, { headers: headers });

  });
}

LOGS

INFO[0004] {
  "remote_ip": "127.0.0.1",,
  "remote_port": 8083,-
  "url": "http://localhost:8083/service-now/writeInSnow",
  "status": 200,
  "status_text": "200 ",
  "proto": "HTTP/1.1",
  "headers": {
    "Content-Type": "application/json",
    "Date": "Tue, 09 Feb 2021 15:33:45 GMT",
    "Vary": "Origin, Access-Control-Request-Method, Access-Control-Request-Headers"
  },
  "cookies": {},
  "body": "{\"results\":[{\"u_estimated_travel_duration\":\"900\",\"w_work_order_task\":null,\"sys_import_state_comment\":\"<response><message/><status>200</status><invalid/><required/><number>WOT0877529</number><sys_id>5e63e0a41b5ee410dd077669cd4bcbcf</sys_id></response>\",\"template_import_log\":\"\",\"sys_updated_on\":\"2021-02-09 15:33:44\",\"u_estimated_work_duration\":\"3600\",\"sys_class_name\":\"u_workordertask_endpoint\",\"sys_target_sys_id\":{\"link\":\"https://totalintegration.service-now.com/api/now/table/wm_task/5e63e0a41b5ee410dd077669cd4bcbcf\",\"value\":\"5e63e0a41b5ee410dd077669cd4bcbcf\"},\"sys_id\":\"5663e0a41b5ee410dd077669cd4bcbca\",\"sys_updated_by\":\"rdv.fuji\",\"u_short_description\":\"\",\"sys_created_on\":\"2021-02-09 15:33:43\",\"u_scheduled_start\":\"2021-02-22 16:00:00\",\"sys_created_by\":\"rdv.fuji\",\"sys_import_row\":\"344\",\"u_assigned_to\":\"1e9f2829e5e33100ea07c36ce50dc80a\",\"u_number\":\"\",\"u_work_order_number\":\"WO0410116\",\"u_work_notes\":\"RDV pris / Appointment taken\",\"sys_target_table\":\"wm_task\",\"u_state\":\"\",\"sys_mod_count\":\"2\",\"u_assignment_group\":\"540cacdd01670200ea079bc64b4a3172\",\"sys_tags\":\"\",\"sys_import_state\":\"inserted\",\"u_category\":\"computer_installation\",\"u_description\":\"\",\"u_service\":\"WOTask\",\"u_business_partner_id\":\"7f62ffe91b955c94a40632649b4bcb3b\",\"u_close_code\":\"\",\"u_operation\":\"submit\"}]}",
  "timings": {
      "duration": 2050.4961,
      "blocked": 3.0007,
      "looking_up": 0,
      "connecting": 1.0019,
      "tls_handshaking": 0,
      "sending": 0.4984,
      "waiting": 2048.4985,
      "receiving": 1.4992
    },
  "tls_version": "",
  "tls_cipher_suite": "",
  "ocsp": {
      "produced_at": 0,
      "this_update": 0,
      "next_update": 0,
      "revoked_at": 0,
      "revocation_reason": "",
      "status": ""
    },
  "error": "",
  "error_code": 0,
  "request": {
      "method": "POST",
      "url": "http://localhost:8083/service-now/writeInSnow",
      "headers": {
        "User-Agent": [
          "k6/0.29.0 (https://k6.io/)"
        ],
        "Content-Type": [
          "application/json"
        ]
      },
      "body": "{\"user_sys_id\":\"904301771b0fd090243b20ad3b4bcb06\",\"tech_sys_id\":\"1e9f2829e5e33100ea07c36ce50dc80a\",\"incident_sys_id\":\"e1bc611e1b396410dd077669cd4bcb43\",\"scheduledStart\":\"2021-02-22 16:00:00\",\"scheduledEnd\":\"2021-02-22 16:45:00\",\"u_state\":17,\"u_operation\":\"submit\",\"comment\":\"RDV pris / Appointment taken\"}",
      "cookies": {}
    }
}  source=console
INFO[0007] {
  "remote_ip": "127.0.0.1",,
  "remote_port": 8083,-
  "url": "http://localhost:8083/service-now/writeInSnow",
  "status": 415,
  "status_text": "415 ",
  "proto": "HTTP/1.1",
  "headers": {
    "Vary": "Origin, Access-Control-Request-Method, Access-Control-Request-Headers",
    "Accept": "application/json, application/hal+json, application/octet-stream, application/xml, application/x-www-form-urlencoded, application/*+json, text/plain, text/xml, application/*+xml, multipart/form-data, multipart/mixed, */*",
    "Content-Length": "0",
    "Date": "Tue, 09 Feb 2021 15:33:48 GMT"
  },
  "cookies": {},
  "body": "",
  "timings": {
      "duration": 9.4968,
      "blocked": 0,
      "looking_up": 0,
      "connecting": 0,
      "tls_handshaking": 0,
      "sending": 0,
      "waiting": 8.998,
      "receiving": 0.4988
    },
  "tls_version": "",
  "tls_cipher_suite": "",
  "ocsp": {
      "produced_at": 0,
      "this_update": 0,
      "next_update": 0,
      "revoked_at": 0,
      "revocation_reason": "",
      "status": ""
    },
  "error": "",
  "error_code": 1415,
  "request": {
      "method": "POST",
      "url": "http://localhost:8083/service-now/writeInSnow",
      "headers": {
        "User-Agent": [
          "k6/0.29.0 (https://k6.io/)"
        ],
        "Content-Type": [
          "application/x-www-form-urlencoded"
        ]
      },
      "body": "{\"user_sys_id\":\"904301771b0fd090243b20ad3b4bcb06\",\"tech_sys_id\":\"1e9f2829e5e33100ea07c36ce50dc80a\",\"incident_sys_id\":\"e1bc611e1b396410dd077669cd4bcb43\",\"scheduledStart\":\"2021-02-22 16:00:00\",\"scheduledEnd\":\"2021-02-22 16:45:00\",\"u_state\":4,\"u_operation\":\"close\",\"comment\":\"RDV pris / Appointment taken\"}",
      "cookies": {}
    }
}  source=console

Any idea what’s the reason for this??

Given that it is 415 Unsupported media type, I would guess that it doesn’t like the fact that it is json.

Your code also does the request twice and the second time you do the request with application/x-www-form-urlencoded, but you don’t print whether that does better ( as well as you don’t print if the second create fails or not).

You should know what the server expects and write the script accordingly.

You can use httpbin to see what k6 sends and maybe even record a session with a browse (if applicable) to not have to write the script by hand.

1 Like

Hi, thanks for your support, I just understood the issue. I was sending empty headers in second request due to that I got ‘415 code’, after refactoring code it works just fine. I create and cancel perfectly. Thanks again for your help!

SCRIPT

import http from 'k6/http';
import { check, sleep ,group} from 'k6';

let options = {
  vus: 1,
  duration: '6s',
};
const SLEEP_DURATION = 3;

export default function () {
    const url = 'http://localhost:8083/service-now/writeInSnow';
    let headers = { 'Content-Type': 'application/json' };
    let data_create = { "user_sys_id": "904301771b0fd090243b20ad3b4bcb06",
    "tech_sys_id": "1e9f2829e5e33100ea07c36ce50dc80a",
    "incident_sys_id": "e1bc611e1b396410dd077669cd4bcb43",
    "scheduledStart": "2021-02-22 16:00:00",
    "scheduledEnd": "2021-02-22 16:45:00",
    "u_state": 17,
    "u_operation": "submit",
                  "comment": "RDV pris / Appointment taken"};
    

  group('secuence',function () {
    // Launching create  
    let res_create = http.request('POST', url, JSON.stringify(data_create), {
        headers: headers,
      });

        console.log(JSON.stringify(res_create, null, "  "));
        check(res_create, {
        'is status 200 create': (r) => r.status === 200,"status is 500 create": r => r.status === 500,});
        sleep(SLEEP_DURATION);

        headers = { 'Content-Type': 'application/x-www-form-urlencoded' };
        res_create = http.request('POST', url, data_create, { headers: headers });

    // Launching cancel  
    let data_cancel = { "user_sys_id": "904301771b0fd090243b20ad3b4bcb06",
    "tech_sys_id": "1e9f2829e5e33100ea07c36ce50dc80a",
    "incident_sys_id": "e1bc611e1b396410dd077669cd4bcb43",
    "scheduledStart": "2021-02-22 16:00:00",
    "scheduledEnd": "2021-02-22 16:45:00",
    "u_state": 4,
    "u_operation": "close",
                  "comment": "RDV pris / Appointment taken"};
    let headers_cancel = { 'Content-Type': 'application/json' };
    let res_cancel = http.request('POST', url, JSON.stringify(data_cancel), {
        headers: headers_cancel,
      });
        console.log(JSON.stringify(res_cancel, null, "  "));
        check(res_cancel, {
        'is status 200 cancel': (r) => r.status === 200,"status is 500 cancel": r => r.status === 500,});
        sleep(SLEEP_DURATION);

        headers = { 'Content-Type': 'application/x-www-form-urlencoded' };
        res_cancel = http.request('POST', url, data_cancel, { headers: headers_cancel});

  });
}
1 Like