Junior — Middle
Can you explain what an HTTP GET request means and how it is used to retrieve data?
sobes.tech AI
Answer from AI
HTTP GET is a request method in the HTTP protocol used to retrieve data from a server. When a client (such as a browser or application) sends a GET request, it asks the server to return a specific resource, such as a web page, JSON data, or a file.
GET requests typically do not contain a request body and pass parameters via the URL (in the query string). They are considered safe and idempotent, meaning they should not alter the server's state.
Example of a GET request in C# using HttpClient:
using System.Net.Http;
using System.Threading.Tasks;
async Task<string> GetDataAsync(string url)
{
using var client = new HttpClient();
var response = await client.GetAsync(url);
response.EnsureSuccessStatusCode();
string data = await response.Content.ReadAsStringAsync();
return data;
}
This code sends a GET request to the specified URL and returns the received data as a string.