Logo

Programming-Idioms

Make an HTTP request with method GET to the URL u, then store the body of the response in the string s.
Implementation
Python

Implementation edit is for fixing errors and enhancing with metadata. Please do not replace the code below with a different implementation.

Instead of changing the code of the snippet, consider creating another Python implementation.

Be concise.

Be useful.

All contributions dictatorially edited by webmasters to match personal tastes.

Please do not paste any copyright violating material.

Please try to avoid dependencies to third-party libraries and frameworks.

Other implementations
import "io"
import "net/http"
res, err := http.Get(u)
if err != nil {
	return err
}
buffer, err := io.ReadAll(res.Body)
res.Body.Close()
if err != nil {
	return err
}
s := string(buffer)
import std.net.curl;
import std.conv;
string s = u.get.to!string;
<script src="https://code.jquery.com/jquery-1.11.3.js"></script>
$.get(u, function(data){
  s = data;
});
$s = file_get_contents($u);
uses fphttpclient;
with TFPHTTPClient.Create(nil) do try
  s := get(u);
finally
  Free;
end;
require 'net/http'
u = URI("http://example.com/index.html")
s = Net::HTTP.get_response(u).body
var xmlHttp = new XMLHttpRequest();
xmlHttp.onreadystatechange = function() { 
	if (xmlHttp.readyState == 4 && xmlHttp.status == 200)
		s = xmlHttp.responseText;
}
xmlHttp.open("GET", u, true);
xmlHttp.send(null);
import urllib.request
with urllib.request.urlopen(u) as f:
    s = f.read()
val s = scala.io.Source.fromURL(u).getLines().mkString("\n")
extern crate reqwest;
use reqwest::Client;
let client = Client::new();
let s = client.get(u).send().and_then(|res| res.text())?;
use HTTP::Tiny qw();
my $s = HTTP::Tiny->new->get($u)->{content};
using System.Net.Http;
var client = new HttpClient();
s = await client.GetStringAsync(u);
[dependencies]
ureq = "1.0"
let s = ureq::get(u).call().into_string()?;
String s = u.text
(def s (slurp u))
[dependencies]
error-chain = "0.12.4"
reqwest = { version = "0.11.2", features = ["blocking"] }

use error_chain::error_chain;
use std::io::Read;
let mut response = reqwest::blocking::get(u)?;
let mut s = String::new();
response.read_to_string(&mut s)?;
fetch(u)
  .then(res => res.text())
  .then(text => s = text)
const res = await fetch(u)
s = await res.text()
(ql:quickload 'dexador)
(defvar *s* (string (dex:get u)))
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
String s = HttpClient.newHttpClient().send(HttpRequest.newBuilder()
                .uri(URI.create(u))
                .GET()
                .build(), HttpResponse.BodyHandlers.ofString())
                .body();