Skip to content

Commit 5c65332

Browse files
Add http.request
1 parent efbabd9 commit 5c65332

6 files changed

+113
-0
lines changed

Diff for: README.md

+15
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,21 @@ Publish a Firebase cloud message
9696

9797
[Example file v8fcm.js](/samples/fcm/v8fcm.js)
9898

99+
## HTTP API <a name="http_api"></a>
100+
* http.request()
101+
102+
**http.request(options)**
103+
Execute a http request
104+
* `options` http request options as JSON string, `String`
105+
106+
107+
[Example file v8http_httpbin_get.js](/samples/http/v8http_httpbin_get.js)
108+
[Example file v8http_httpbin_post.js](/samples/http/v8http_httpbin_post.js)
109+
[Example file v8http_httpbin_basicauth.js](/samples/http/v8http_httpbin_basicauth.js)
110+
[Example file v8http_httpbin_bearerauth.js](/samples/http/v8http_httpbin_bearerauth.js)
111+
[Example file v8http_httpbin_headers.js](/samples/http/v8http_httpbin_headers.js)
112+
113+
99114
## Examples <a name="examples"></a>
100115
## Input form example <a name="example_input"></a>
101116
![Alt text](/screenshots/v8input.gif?raw=true "Input form")

Diff for: samples/http/v8http_httpbin_basicauth.js

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//http basic auth example
2+
function main() {
3+
rq = {
4+
url: "http://httpbin.org/basic-auth/myuser/mypasswd",
5+
method: "GET",
6+
basicAuth: {
7+
username: "myuser",
8+
password: "mypasswd"
9+
}
10+
}
11+
12+
x = http.request(JSON.stringify(rq))
13+
console.log(x.statuscode)
14+
console.log(x.content)
15+
16+
rq.basicAuth.password = "wrong password";
17+
x = http.request(JSON.stringify(rq))
18+
console.error(x.statuscode)
19+
20+
return ""
21+
};
22+

Diff for: samples/http/v8http_httpbin_bearerauth.js

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//http bearer authorization example
2+
function main() {
3+
rq = {
4+
url: "http://httpbin.org/bearer",
5+
method: "GET",
6+
authorization: "Bearer Bearertokenxyz"
7+
}
8+
9+
x = http.request(JSON.stringify(rq))
10+
11+
console.log(x.statuscode)
12+
console.log(x.content)
13+
14+
return ""
15+
}
16+

Diff for: samples/http/v8http_httpbin_get.js

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//http get example with parameters
2+
function main() {
3+
rq = {
4+
url: "http://httpbin.org/anything",
5+
method: "GET",
6+
queryParams: [
7+
{ "key": "hello",
8+
"value": "world"
9+
},{
10+
"key": "hello1",
11+
"value": "world2"
12+
}]
13+
}
14+
15+
x = http.request(JSON.stringify(rq))
16+
console.log(x.statuscode)
17+
console.log(x.content)
18+
19+
return JSON.parse(x.content).url
20+
};
21+
22+
23+

Diff for: samples/http/v8http_httpbin_headers.js

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//http header example
2+
function main() {
3+
rq = {
4+
url: "https://httpbin.org/headers",
5+
method: "GET",
6+
headerData: [
7+
{key: "X-Timezone-Iana", value: "Europe/Berlin"}
8+
]
9+
}
10+
11+
x = http.request(JSON.stringify(rq))
12+
console.log(x.statuscode)
13+
console.log(x.content)
14+
15+
return x.statuscode
16+
};
17+
18+
19+

Diff for: samples/http/v8http_httpbin_post.js

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//http post example with body
2+
function main() {
3+
rq = {
4+
url: "https://httpbin.org/anything",
5+
contentType: "application/json; charset=UTF-8",
6+
method: "POST",
7+
rawData: "{\"id\":42, \"myval\":\"Hello world\"}"
8+
}
9+
10+
x = http.request(JSON.stringify(rq))
11+
console.log(x.statuscode)
12+
console.log(x.content)
13+
14+
return JSON.parse(x.content).data
15+
};
16+
17+
18+

0 commit comments

Comments
 (0)