HTTP: Difference between revisions

1,482 bytes added ,  5 months ago
m
m (→‎{{header|Wren}}: Minor tidy)
 
(6 intermediate revisions by 4 users not shown)
Line 1,194:
<syntaxhighlight lang="frink">
print[read["http://frinklang.org/"]]
</syntaxhighlight>
 
=={{header|FutureBasic}}==
<syntaxhighlight lang="javascriptfuturebasic">
include "NSLog.incl"
 
#plist NSAppTransportSecurity @{NSAllowsArbitraryLoads:YES}
 
local fn URLSessionHandler( session as URLSessionRef, dta as CFDataRef, response as URLResponseRef, err as ErrorRef, userData as ptr )
if ( fn HTTPURLResponseStatusCode( (HTTPURLResponseRef)response ) == 200 )
NSLog( @"%@", fn StringWithData( dta, NSUTF8StringEncoding ) )
else
NSLog( @"%@", fn ErrorLocalizedDescription( err ) )
end if
NSLogScrollToTop
end fn
 
local fn URLSessionWithGetRequest( path as CFStringRef )
CFURLRef url = fn URLWithString( path )
MutableURLRequestRef urlRequest = fn MutableURLRequestWithURL( url )
MutableURLRequestSetHTTPMethod( urlRequest, @"HTTP" )
URLSessionRef session = fn URLSessionSharedSession
URLSessionDataTaskRef task = fn URLSessionDataTaskWithRequestCompletionHandler( session, urlRequest, @fn URLSessionHandler, NULL )
URLSessionTaskResume( task )
end fn
 
fn URLSessionWithGetRequest( @"http://rosettacode.org" )
 
HandleEvents
</syntaxhighlight>
 
Line 1,382 ⟶ 1,411:
 
===Browser===
Using fetch API and async/await:
<syntaxhighlight lang="javascript">
const response = await fetch('http://rosettacode.org');
const text return= await response.text();
console.log(myTexttext);
</syntaxhighlight>
 
Using fetch API:
<syntaxhighlight lang="javascript">
fetch('http://rosettacode.org').then(function (response) {
return response.text();
}).then(function (myTexttext) {
console.log(text);
});
</syntaxhighlight>
 
<syntaxhighlight lang="javascript">var req = new XMLHttpRequest();
req.onload = function() {
Line 1,389 ⟶ 1,434:
req.open('get', 'http://rosettacode.org', true);
req.send()</syntaxhighlight>
 
Using fetch API:
<syntaxhighlight lang="javascript">
fetch('http://rosettacode.org').then(function(response) {
return response.text();
}).then(function(myText) {
console.log(myText);
});
</syntaxhighlight>
 
As a repeatable function:
Line 2,250 ⟶ 2,286:
 
=={{header|PowerShell}}==
<syntaxhighlight lang="powershell">
Invoke-WebRequest 'http://www.rosettacode.org'
</syntaxhighlight>
 
<syntaxhighlight lang="powershell">
$wc = New-Object Net.WebClient
Line 2,603 ⟶ 2,643:
=={{header|Sidef}}==
Sidef can load and use Perl modules:
<syntaxhighlight lang="ruby">func getrequire(url'HTTP::Tiny') {
 
var lwp = (
func get(url) {
try { require('LWP::UserAgent') }
varstatic ua = lwp%O<HTTP::Tiny>.new(agent => 'Mozilla/5.0')
catch { warn "'LWP::UserAgent' is not installed!"; return nil }
var lwpresp = ua.get(url)
)
if (resp{:success}) {
var ua = lwp.new(agent => 'Mozilla/5.0')
if (var resp = ua.get(url);return resp{:content}.is_success) {decode_utf8
return resp.decoded_content
}
return nil
}
 
printsay get("http://rosettacode.org")</syntaxhighlight>
 
=={{header|Smalltalk}}==
Line 2,762 ⟶ 2,801:
Dim content As String = client.DownloadString("http://www.google.com")
Console.WriteLine(content)
</syntaxhighlight>
 
=={{header|V (Vlang)}}==
<syntaxhighlight lang="Vlang">
import net.http
 
fn main() {
resp := http.get("http://rosettacode.org/robots.txt") or {println(err) exit(-1)}
println(resp.body.str())
}
</syntaxhighlight>
 
Line 2,768 ⟶ 2,817:
{{libheader|libcurl}}
An embedded program so we can ask the C host to communicate with libcurl for us.
<syntaxhighlight lang="ecmascriptwren">/* httpHTTP.wren */
 
var CURLOPT_URL = 10002
Line 2,801 ⟶ 2,850:
<br>
We now embed this in the following C program, compile and run it.
<syntaxhighlight lang="c">/* gcc httpHTTP.c -o httpHTTP -lcurl -lwren -lm */
 
#include <stdio.h>
Line 2,911 ⟶ 2,960:
WrenVM* vm = wrenNewVM(&config);
const char* module = "main";
const char* fileName = "httpHTTP.wren";
char *script = readFile(fileName);
WrenInterpretResult result = wrenInterpret(vm, module, script);
9,476

edits