How do I make an HTTP request in Javascript? · The Chat Archive
Explore / Human feedback How do I make an HTTP request in Javascript? Save Share Download ▾ ReportHow do I make an HTTP request in Javascript?
To make an HTTP request in JavaScript, you can use the built-in XMLHttpRequest object or the fetch API, which is a more modern and simpler way to make requests. Here's an example using both methods:
Method 1: Using XMLHttpRequest
function makeHttpGetRequest(url, callback) {
const xhr = new XMLHttpRequest();
xhr.open('GET', url, true);
xhr.onload = function() {
if (xhr.status >= 200 && xhr.status < 400) {
callback(xhr.responseText);
} else {
console.error(`Error: ${xhr.status} - ${xhr.statusText}`);
}
};
xhr.onerror = function() {
console.error('An error occurred during the request.');
};
xhr.send();
}
// Usage
makeHttpGetRequest('https://api.example.com/data', (response) => {
console.log(response);
});
Sure, I'd be happy to provide more examples. Here's an example of making a PUT request using the fetch API to update a user's information:
function makeHttpPutRequest(url, data) {
return fetch(url, {
method: 'PUT',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(data),
})
.then((response) => {
if (response.ok) {
return response.json();
} else {
throw new Error(`Error: ${response.status} - ${response.statusText}`);
}
})
.then((data) => {
console.log(data);
})
.catch((error) => {
console.error(error);
});
}
// Usage
const userId = 123;
const updatedUserData = { name: 'Jane Doe', age: 35 };
makeHttpPutRequest(`https://api.example.com/users/${userId}`, updatedUserData);
Copy first prompt Copy first response
Other models, same or similar prompt Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Details
Model Unknown
Provider unknown
Model provenance As recorded by source dataset
Turns 2 (4 messages)
Language —
Added to archive Nov 22, 2026
ID 01m80v161ct6ajz9k3b3mqymb7Source & license Imported from NVIDIA HelpSteer3 Preference (source ). Imported records earn no Research Points.
Upstream record: preference/train.jsonl.gz:8381:response2
nvidia/HelpSteer3, revision f6d145777bcbde96137596340fab89793acd1031. Original NVIDIA preference release. Each response becomes a separate transcript retaining its shared context. Generator identity is not supplied per row. Dialogue normalized; system instructions, tools and private metadata omitted.
License: CC-BY-4.0
Dataset releases Not yet included in a published release.
Method 2: Using fetch API
function makeHttpGetRequest(url) {
return fetch(url)
.then((response) => {
if (response.ok) {
return response.text();
} else {
throw new Error(`Error: ${response.status} - ${response.statusText}`);
}
})
.then((data) => {
console.log(data);
})
.catch((error) => {
console.error(error);
});
}
// Usage
makeHttpGetRequest('https://api.example.com/data');
Both methods support other HTTP methods like POST, PUT, DELETE, etc. For POST requests, you can send data in the request body. Here's an example using the fetch API:
function makeHttpPostRequest(url, data) {
return fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(data),
})
.then((response) => {
if (response.ok) {
return response.json();
} else {
throw new Error(`Error: ${response.status} - ${response.statusText}`);
}
})
.then((data) => {
console.log(data);
})
.catch((error) => {
console.error(error);
});
}
// Usage
const data = { name: 'John Doe', age: 30 };
makeHttpPostRequest('https://api.example.com/users', data);
Here's an example of making a DELETE request using the fetch API to delete a user:
function makeHttpDeleteRequest(url) {
return fetch(url, {
method: 'DELETE',
})
.then((response) => {
if (response.ok) {
return response.json();
} else {
throw new Error(`Error: ${response.status} - ${response.statusText}`);
}
})
.then((data) => {
console.log(data);
})
.catch((error) => {
console.error(error);
});
}
// Usage
const userId = 123;
makeHttpDeleteRequest(`https://api.example.com/users/${userId}`);
And here's an example of making a GET request with query parameters using the fetch API:
function makeHttpGetRequestWithQueryParams(url, params) {
const queryString = new URLSearchParams(params).toString();
const requestUrl = `${url}?${queryString}`;
return fetch(requestUrl)
.then((response) => {
if (response.ok) {
return response.json();
} else {
throw new Error(`Error: ${response.status} - ${response.statusText}`);
}
})
.then((data) => {
console.log(data);
})
.catch((error) => {
console.error(error);
});
}
// Usage
const params = {
page: 1,
limit: 10,
sort: 'name',
};
makeHttpGetRequestWithQueryParams('https://api.example.com/users', params);