How to store and work with REST post response?

Hello guys,

I am new to Codeceptjs with WebdriverIO and JavaScript and I just started to work with REST helper. I was wondering if there a way to store post response and retrieve values from it?

I would be really grateful if you could share your examples.

You can get access to the data object on the response, like so

1 Like

Yes, the examples are referenced from CodeceptJS docs: https://codecept.io/examples/#rest-example-tests
result.status contains HTTP response (e.g. 200 which means O.K.) and result.data contains response (e.g. JSON response).
More details on https://github.com/axios/axios#response-schema (CodeceptJS uses axios library to perform REST requests)

Thank you a lot guys. Finally I was able to store response. Now it looks like this:

var response = await I.sendPostRequest(url, postBody);
var users = response.data;
console.log(users);

Console output:

[
{id:‘001c’, name:‘Max’, city:‘London’},
{id:‘002c’, name:‘Tom’, city:‘Bristol’}
]

My question is, how can I access name field in one or all records? For example if I want to verify it equals expected value?

Ex.
expect(users.name).to.eql(‘Max’);

expect(users[0].name).to.equal(“Max”)

1 Like

Hello guys,

I am trying the same to fetch the response but somehow I get this error message:- ‘Property ‘status’ does not exist on type ‘void’.ts(2339)’

I see the return type of the I.sendGetRequest(’/rtos’) to be void.

Sample code:-

const res = await I.sendGetRequest(’/rtos’);
expect(res.status).to.eql(200); --> GIves compile time error on this line.

Please help me on this.

Thanks,
Deven J.

Hello Team,

Can someone please help me here ?