Api testing using GOT

 

We all know that request library has been deprecated , so I was searching for a new and better API library , as part of that I found GOT


Please refer to below link for the cool features of GOT


https://www.npmjs.com/package/got#comparison


Installation of GOT package:

use: npm install got


Sample Program for get call  in TypeScript:


import got from 'got';


export class A {

static async f1() {

const burl = 'https://xyzabctest.com';

    //Way 1
const res1 = Promise.resolve(got.get(burl));
    //Way 2
const res2 = await got.get(burl);
    //Way 3
    const res4 = await got(burl);


await console.log("t1------------------------------>"+res1.then((t)=>{console.log(t.body)}));

await console.log("t4------------------------------>"+res2.body)

await console.log("t5------------------------------>"+res4.body)

const res5 = res1.then((t)=>{return t.body})

await console.log("t6------------------------------>"+res5)
}

}
A.f1();


POST CALL :
/***
* Program for POST Call
*/
const authurl = 'https://xyz/api/login';
const res3= await got.post(authurl,{json:{
"username": "f",
"password": "df@123",
"service": "forecasting",
responseType: json5
} })
await console.log("tupper3------------------------------>"+res3.body);

/**
* Program for post call using Promise
*/
const body1= Promise.resolve( got.post(authurl,{json:{
"username": "ts",
"password": "dfdf@123",
"service": "fd"}}).json());
await console.log('temp---------->'+body1)
await console.log("tupper------------------------------>"+body1.then((t)=>{console.log(t)}));


Comments