Can't parse response after GET request

Hi, I try to create the test case for API. I send my GET request, next I would like to parse my response, but I see an error on my console. I use the same method for parse elements for my POST request and everything is ok. I have a problem with GET.

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

export const options = { vus: 1, duration: '1s' }

export default function() {

    let res = http.request('GET', "https://restful-booker.herokuapp.com/booking/11496", {
        headers: { "Accept": "application/json" }});

    
    let response = JSON.parse(res.body);
    console.log(response);

}

Hi @Mada

From your example, I see a couple of issues.

First, the request( method, url, [body], [params] ) has a body, that in your case should be null.

I ran a curl to get an existing record:

curl https://restful-booker.herokuapp.com/booking/6366

Which returns:

{"firstname":"John","lastname":"Smith","totalprice":111,"depositpaid":true,"bookingdates":{"checkin":"2018-01-01","checkout":"2019-01-01"},"additionalneeds":"Breakfast"}

And changed the test script:

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

export const options = { vus: 1, duration: '1s' }

export default function () {

    const res = http.request("GET", "https://restful-booker.herokuapp.com/booking/6366", null, {
        headers: { "Accept": "application/json" }
    });

    let response = JSON.parse(res.body);
    console.log(response);
}

Which now returns correctly, at least for me:

 
          /\      |‾‾| /‾‾/   /‾‾/   
     /\  /  \     |  |/  /   /  /    
    /  \/    \    |     (   /   ‾‾\  
   /          \   |  |\  \ |  (‾)  | 
  / __________ \  |__| \__\ \_____/ .io

  execution: local
     script: heroku.js
     output: -

  scenarios: (100.00%) 1 scenario, 1 max VUs, 31s max duration (incl. graceful stop):
           * default: 1 looping VUs for 1s (gracefulStop: 30s)

INFO[0000] {"firstname":"John","lastname":"Smith","totalprice":111,"depositpaid":true,"bookingdates":{"checkin":"2018-01-01","checkout":"2019-01-01"},"additionalneeds":"Breakfast"}  source=console
INFO[0000] {"firstname":"John","lastname":"Smith","totalprice":111,"depositpaid":true,"bookingdates":{"checkin":"2018-01-01","checkout":"2019-01-01"},"additionalneeds":"Breakfast"}  source=console
INFO[0001] {"firstname":"John","lastname":"Smith","totalprice":111,"depositpaid":true,"bookingdates":{"checkin":"2018-01-01","checkout":"2019-01-01"},"additionalneeds":"Breakfast"}  source=console
INFO[0001] {"firstname":"John","lastname":"Smith","totalprice":111,"depositpaid":true,"bookingdates":{"checkin":"2018-01-01","checkout":"2019-01-01"},"additionalneeds":"Breakfast"}  source=console
INFO[0001] {"firstname":"John","lastname":"Smith","totalprice":111,"depositpaid":true,"bookingdates":{"checkin":"2018-01-01","checkout":"2019-01-01"},"additionalneeds":"Breakfast"}  source=console

running (01.1s), 0/1 VUs, 5 complete and 0 interrupted iterations
default ✓ [======================================] 1 VUs  1s

     data_received..................: 7.7 kB 6.9 kB/s
     data_sent......................: 1.2 kB 1.1 kB/s
     http_req_blocked...............: avg=98.15ms  min=2µs      med=7µs      max=490.73ms p(90)=294.44ms p(95)=392.58ms
     http_req_connecting............: avg=21.66ms  min=0s       med=0s       max=108.3ms  p(90)=64.98ms  p(95)=86.64ms 
     http_req_duration..............: avg=122.13ms min=117.32ms med=119.56ms max=132.98ms p(90)=128.48ms p(95)=130.73ms
       { expected_response:true }...: avg=122.13ms min=117.32ms med=119.56ms max=132.98ms p(90)=128.48ms p(95)=130.73ms
     http_req_failed................: 0.00%  ✓ 0        ✗ 5  
     http_req_receiving.............: avg=102.2µs  min=43µs     med=64µs     max=210µs    p(90)=186µs    p(95)=197.99µs
     http_req_sending...............: avg=244.99µs min=9µs      med=18µs     max=1.15ms   p(90)=704.6µs  p(95)=927.79µs
     http_req_tls_handshaking.......: avg=47.85ms  min=0s       med=0s       max=239.27ms p(90)=143.56ms p(95)=191.41ms
     http_req_waiting...............: avg=121.78ms min=117.24ms med=119.49ms max=132.93ms p(90)=128.38ms p(95)=130.65ms
     http_reqs......................: 5      4.528666/s
     iteration_duration.............: avg=220.69ms min=117.56ms med=122.21ms max=610.75ms p(90)=419.7ms  p(95)=515.23ms
     iterations.....................: 5      4.528666/s
     vus............................: 1      min=1      max=1
     vus_max........................: 1      min=1      max=1

There is another issue with this that can be impacting if you were to use nonexistent records. For example:

curl https://restful-booker.herokuapp.com/booking/712124

Which returns a 404 not found:

Not Found%

Would cause the script to return the error you see as well.

The line let response = JSON.parse(res.body); will throw an error when the response has no body.

You should probably add a check there to see the ones that fail. Similar to:

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

export const options = { vus: 1, duration: '1s' }

export default function () {

    const res = http.request("GET", "https://restful-booker.herokuapp.com/booking/712124", null, {
        headers: { "Accept": "application/json" }
    });

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

    if (res.status === 200) {
        let response = JSON.parse(res.body);
        console.log(response);
    }
}

This will fail if the record does not exist:

And will work if it does:

Finally, note that debugging can be useful when troubleshooting those cases. You would be able to see the http responses and figure out what the issue was.

Let me know if that helps.

Cheers!

1 Like