snippets_js
Integration
New integration from credential
const axiosClient = yepcode.integration.http('credential-slug')
New integration from plain authentication data
const axios = require('axios');
const axiosClient = axios.create({
baseURL: 'https://some-domain.com/api/',
timeout: 1000,
auth: {
username: 'user',
password: 'password',
},
headers: {'X-Custom-Header': 'foobar'}
});
GET
GET (async/await)
try {
const response = await axiosClient.get('/users/12345')
console.log('Response status: ', response.status);
console.log('User: ', response.data);
console.log(response.statusText);
console.log(response.headers);
console.log(response.config);
} catch (error) {
if (error.response) {
// The request was made and the server responded with a status code
// that falls out of the range of 2xx
console.error(error.response);
} else if (error.request) {
// The request was made but no response was received
// `error.request` is an instance of XMLHttpRequest in the browser and an instance of
// http.ClientRequest in node.js
console.error(error.request);
} else {
// Something happened in setting up the request that triggered an Error
console.log('Error', error.message);
}
console.error(error.config);
throw error
};
GET (promise)
axiosClient.get('/users/12345')
.then((response) => {
console.log('Response status: ', response.status);
console.log('User: ', response.data);
console.log(response.statusText);
console.log(response.headers);
console.log(response.config);
}).catch((error) => {
if (error.response) {
// The request was made and the server responded with a status code
// that falls out of the range of 2xx
console.error(error.response);
} else if (error.request) {
// The request was made but no response was received
// `error.request` is an instance of XMLHttpRequest in the browser and an instance of
// http.ClientRequest in node.js
console.error(error.request);
} else {
// Something happened in setting up the request that triggered an Error
console.log('Error', error.message);
}
console.error(error.config);
throw error
});
GET (callback)
axiosClient.get('/users/12345', (error, response) => {
if (error) {
if (error.response) {
// The request was made and the server responded with a status code
// that falls out of the range of 2xx
console.error(error.response);
} else if (error.request) {
// The request was made but no response was received
// `error.request` is an instance of XMLHttpRequest in the browser and an instance of
// http.ClientRequest in node.js
console.error(error.request);
} else {
// Something happened in setting up the request that triggered an Error
console.log('Error', error.message);
}
console.error(error.config);
} else {
console.log('Response status: ', response.status);
console.log('User: ', response.data);
console.log(response.statusText);
console.log(response.headers);
console.log(response.config);
}
})
POST
POST (async/await)
try {
const response = await axiosClient.post('/users/12345', {
firstName: 'New first name',
lastName: 'New last name'
})
console.log('Response status: ', response.status);
console.log('User was updated successfully: ', JSON.stringify(response.data.user, null, 2));
} catch (error) {
if (error.response) {
// The request was made and the server responded with a status code
// that falls out of the range of 2xx
console.error(error.response);
} else if (error.request) {
// The request was made but no response was received
// `error.request` is an instance of XMLHttpRequest in the browser and an instance of
// http.ClientRequest in node.js
console.error(error.request);
} else {
// Something happened in setting up the request that triggered an Error
console.log('Error', error.message);
}
console.error(error.config);
throw error
}
POST (Promise)
axiosClient.post('/users/12345', {
firstName: 'New first name',
lastName: 'New last name'
})
.then(function (response) {
console.log('Response status: ', response.status);
console.log('User was updated successfully: ', JSON.stringify(response.data.user, null, 2));
})
.catch(function (error) {
if (error.response) {
// The request was made and the server responded with a status code
// that falls out of the range of 2xx
console.error(error.response);
} else if (error.request) {
// The request was made but no response was received
// `error.request` is an instance of XMLHttpRequest in the browser and an instance of
// http.ClientRequest in node.js
console.error(error.request);
} else {
// Something happened in setting up the request that triggered an Error
console.log('Error', error.message);
}
console.error(error.config);
})
POST (callback)
axiosClient.post('/users/12345', {
firstName: 'New first name',
lastName: 'New last name'
}, (error, response) => {
if (error) {
if (error.response) {
// The request was made and the server responded with a status code
// that falls out of the range of 2xx
console.error(error.response);
} else if (error.request) {
// The request was made but no response was received
// `error.request` is an instance of XMLHttpRequest in the browser and an instance of
// http.ClientRequest in node.js
console.error(error.request);
} else {
// Something happened in setting up the request that triggered an Error
console.log('Error', error.message);
}
console.error(error.config);
} else {
console.log('Response status: ', response.status);
console.log('User was updated successfully: ', JSON.stringify(response.data.user, null, 2));
}
})
PUT
PUT (async/await)
try {
const response = await axiosClient.put('/users/12345', {
firstName: 'New first name',
lastName: 'New last name'
})
console.log('Response status: ', response.status);
console.log('User was updated successfully: ', JSON.stringify(response.data.user, null, 2));
} catch (error) {
if (error.response) {
// The request was made and the server responded with a status code
// that falls out of the range of 2xx
console.error(error.response);
} else if (error.request) {
// The request was made but no response was received
// `error.request` is an instance of XMLHttpRequest in the browser and an instance of
// http.ClientRequest in node.js
console.error(error.request);
} else {
// Something happened in setting up the request that triggered an Error
console.log('Error', error.message);
}
console.error(error.config);
throw error
}
PUT (Promise)
axiosClient.put('/users/12345', {
firstName: 'New first name',
lastName: 'New last name'
})
.then(function (response) {
console.log('Response status: ', response.status);
console.log('User was updated successfully: ', JSON.stringify(response.data.user, null, 2));
})
.catch(function (error) {
if (error.response) {
// The request was made and the server responded with a status code
// that falls out of the range of 2xx
console.error(error.response);
} else if (error.request) {
// The request was made but no response was received
// `error.request` is an instance of XMLHttpRequest in the browser and an instance of
// http.ClientRequest in node.js
console.error(error.request);
} else {
// Something happened in setting up the request that triggered an Error
console.log('Error', error.message);
}
console.error(error.config);
})
PUT (callback)
axiosClient.put('/users/12345', {
firstName: 'New first name',
lastName: 'New last name'
}, (error, response) => {
if (error) {
if (error.response) {
// The request was made and the server responded with a status code
// that falls out of the range of 2xx
console.error(error.response);
} else if (error.request) {
// The request was made but no response was received
// `error.request` is an instance of XMLHttpRequest in the browser and an instance of
// http.ClientRequest in node.js
console.error(error.request);
} else {
// Something happened in setting up the request that triggered an Error
console.log('Error', error.message);
}
console.error(error.config);
} else {
console.log('Response status: ', response.status);
console.log('User was updated successfully: ', JSON.stringify(response.data.user, null, 2));
}
})
DELETE
DELETE (async/await)
try {
const response = await axiosClient.delete('/users/12345')
console.log('Response status: ', response.status);
console.log('User: ', response.data);
console.log(response.statusText);
console.log(response.headers);
console.log(response.config);
} catch (error) {
if (error.response) {
// The request was made and the server responded with a status code
// that falls out of the range of 2xx
console.error(error.response);
} else if (error.request) {
// The request was made but no response was received
// `error.request` is an instance of XMLHttpRequest in the browser and an instance of
// http.ClientRequest in node.js
console.error(error.request);
} else {
// Something happened in setting up the request that triggered an Error
console.log('Error', error.message);
}
console.error(error.config);
throw error
};
DELETE (promise)
axiosClient.delete('/users/12345')
.then((response) => {
console.log('Response status: ', response.status);
console.log('User: ', response.data);
console.log(response.statusText);
console.log(response.headers);
console.log(response.config);
}).catch((error) => {
if (error.response) {
// The request was made and the server responded with a status code
// that falls out of the range of 2xx
console.error(error.response);
} else if (error.request) {
// The request was made but no response was received
// `error.request` is an instance of XMLHttpRequest in the browser and an instance of
// http.ClientRequest in node.js
console.error(error.request);
} else {
// Something happened in setting up the request that triggered an Error
console.log('Error', error.message);
}
console.error(error.config);
throw error
});
DELETE (callback)
axiosClient.delete('/users/12345', (error, response) => {
if (error) {
if (error.response) {
// The request was made and the server responded with a status code
// that falls out of the range of 2xx
console.error(error.response);
} else if (error.request) {
// The request was made but no response was received
// `error.request` is an instance of XMLHttpRequest in the browser and an instance of
// http.ClientRequest in node.js
console.error(error.request);
} else {
// Something happened in setting up the request that triggered an Error
console.log('Error', error.message);
}
console.error(error.config);
} else {
console.log('Response status: ', response.status);
console.log('User: ', response.data);
console.log(response.statusText);
console.log(response.headers);
console.log(response.config);
}
})
Cancellation
Cancellation
const axios, { CancelToken } = require('axios');
const source = CancelToken.source();
axiosClient.get('/user/12345', {
cancelToken: source.token
}).catch((thrown) => {
if (axios.isCancel(thrown)) {
console.log('Request canceled', thrown.message);
} else if (error.response) {
// The request was made and the server responded with a status code
// that falls out of the range of 2xx
console.error(error.response);
} else if (error.request) {
// The request was made but no response was received
// `error.request` is an instance of XMLHttpRequest in the browser and an instance of
// http.ClientRequest in node.js
console.error(error.request);
} else {
// Something happened in setting up the request that triggered an Error
console.log('Error', error.message);
}
});
source.cancel('Operation canceled by the user.');