-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathhttp.ts
More file actions
57 lines (49 loc) · 1.28 KB
/
http.ts
File metadata and controls
57 lines (49 loc) · 1.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import {
Client,
ClientConfigBuilder,
} from "@polywrap/client-js";
import { httpPlugin } from "@polywrap/http-plugin-js";
import { Uri } from "@polywrap/core-js";
interface Response {
status: Number;
statusText: String;
headers: Map<String, String> | undefined;
body: String | undefined;
}
const { uri } = Uri.from("wrapscan.io/polywrap/http@1.0");
const main = async () => {
const builder = new ClientConfigBuilder();
builder.setPackage(uri, httpPlugin({}));
const client = new Client(builder.build());
const getResult = await client.invoke<Response>({
uri,
method: "get",
args: {
url: "https://httpbin.org/get",
},
});
if (!getResult.ok) {
throw Error("Get method error: " + getResult.error);
}
console.log("Get method response: ", getResult.value);
const postResult = await client.invoke<Response>({
uri,
method: "post",
args: {
url: "https://httpbin.org/post",
request: {
body: JSON.stringify({ item: "Hello world!" }),
responseType: "TEXT",
},
},
});
if (!postResult.ok) {
throw Error("Post method error: " + postResult.error);
}
console.log("Post method response: ", postResult.value);
};
main()
.then()
.catch((e) => {
console.error("Error in HTTP example: ", e);
});