This language bar is your friend. Select your favorite languages!

Idiom #101 Load from HTTP GET request into a string

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

<script src="https://code.jquery.com/jquery-1.11.3.js"></script>
$.get(u, function(data){
  s = data;
});
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 std.net.curl;
import std.conv;
string s = u.get.to!string;
import "io/ioutil"
import "net/http"
res, err := http.Get(u)
if err != nil {
	return err
}
buffer, err := ioutil.ReadAll(res.Body)
res.Body.Close()
if err != nil {
	return err
}
s := string(buffer)
$s = file_get_contents($u);
uses fphttpclient;
with TFPHTTPClient.Create(nil) do try
  s := get(u);
finally
  Free;
end;
import urllib.request
with urllib.request.urlopen(u) as f:
    s = f.read()
require 'net/http'
u = URI("http://example.com/index.html")
s = Net::HTTP.get_response(u).body
val s = scala.io.Source.fromURL(u).getLines().mkString("\n")

Do you know the best way to do this in your language ?
New implementation...

Idiom created by

programming-idioms.org

Related idioms