Skip to main content

HTTP

Send HTTP requests to any endpoint

Tagsresthttpapi
Network Connection needs

This integration needs network access to the server where the service is running.

See the Network access page for details about how to achieve that.

Credential configuration

Complete the form with the baseURL, and include username and password if necessary. In the JSON field, add the HTTP headers you want to be sent in all requests.

Here is an example of a filled credential configuration form in YepCode:

HTTP Snippets available in Editor

note

The title is the triggering text for YepCode to autocomplete the script.

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.');