This language bar is your friend. Select your favorite languages!
Select your favorite languages :
- Or search :
- Clojure
- C#
- D
- Go
- Groovy
- JS
- JS
- JS
- JS
- Java
- Java
- Lisp
- PHP
- Pascal
- Perl
- Python
- Python
- Ruby
- Rust
- Rust
- Rust
- Scala
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)
res has type *http.Response.
buffer has type []byte.
It is idiomatic and strongly recommended to check errors at each step.
buffer has type []byte.
It is idiomatic and strongly recommended to check errors at each step.
$.get(u, function(data){
s = data;
});
Uses the jQuery library.
fetch(u)
.then(res => res.text())
.then(text => s = text)
Fetch is a relatively new API and isn't available in IE. A polyfill can be found here: https://github.com/github/fetch
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);
This is asynchronous.
const res = await fetch(u)
s = await res.text()
Fetch is a relatively new API and isn't available in IE. A polyfill can be found here: https://github.com/github/fetch
Async/await is also an ES2017 feature.
Async/await is also an ES2017 feature.
HttpsURLConnection h = null;
try {
h = (HttpsURLConnection) u.openConnection();
h.setRequestMethod("GET");
byte a[] = h.getInputStream().readAllBytes();
String s = new String(a);
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
if (h != null) h.disconnect();
}
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();
s = requests.get(u).content.decode()
installing requests library: pip install requests
[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)?;
This is a synchronous (blocking) reqwest call.
The [dependencies] section goes to cargo.toml. The optional feature blocking must be explicit.
The [dependencies] section goes to cargo.toml. The optional feature blocking must be explicit.
let client = Client::new();
let s = client.get(u).send().and_then(|res| res.text())?;