7.web服务器中收发REST接口
1.web服务器中收发REST接口
1.C语言在linux中创建web服务器收发消息
server.c
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
void send_cors_headers(int client) {
dprintf(client,
"Access-Control-Allow-Origin: *\r\n"
"Access-Control-Allow-Methods: GET, POST, OPTIONS\r\n"
"Access-Control-Allow-Headers: Content-Type\r\n"
);
}
int main() {
int server_fd = socket(AF_INET, SOCK_STREAM, 0);
int opt = 1;
setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
struct sockaddr_in addr = {
.sin_family = AF_INET,
.sin_port = htons(3000),
.sin_addr.s_addr = INADDR_ANY
};
if (bind(server_fd, (struct sockaddr*)&addr, sizeof(addr)) < 0) {
perror("bind");
return 1;
}
if (listen(server_fd, 10) < 0) {
perror("listen");
return 1;
}
printf("REST server running at http://localhost:3000\n");
while (1) {
int client = accept(server_fd, NULL, NULL);
char buf[4096] = {0};
read(client, buf, sizeof(buf) - 1);
char method[8], path[128];
sscanf(buf, "%s %s", method, path);
// OPTIONS
if (strcmp(method, "OPTIONS") == 0) {
dprintf(client,
"HTTP/1.1 204 No Content\r\n"
);
send_cors_headers(client);
dprintf(client, "\r\n");
close(client);
continue;
}
// GET /hello
if (strcmp(method, "GET") == 0 && strcmp(path, "/hello") == 0) {
const char *body = "{\"message\":\"Hello REST\"}";
dprintf(client,
"HTTP/1.1 200 OK\r\n"
);
send_cors_headers(client);
dprintf(client,
"Content-Type: application/json\r\n"
"Content-Length: %zu\r\n"
"\r\n"
"%s",
strlen(body), body
);
close(client);
continue;
}
// POST /echo
if (strcmp(method, "POST") == 0 && strcmp(path, "/echo") == 0) {
char *body = strstr(buf, "\r\n\r\n");
body = body ? body + 4 : "";
char response[4096];
snprintf(response, sizeof(response),
"{\"you_sent\":\"%s\"}", body);
dprintf(client,
"HTTP/1.1 200 OK\r\n"
);
send_cors_headers(client);
dprintf(client,
"Content-Type: application/json\r\n"
"Content-Length: %zu\r\n"
"\r\n"
"%s",
strlen(response), response
);
close(client);
continue;
}
// 404
const char *body = "{\"error\":\"Not Found\"}";
dprintf(client,
"HTTP/1.1 404 Not Found\r\n"
);
send_cors_headers(client);
dprintf(client,
"Content-Type: application/json\r\n"
"Content-Length: %zu\r\n"
"\r\n"
"%s",
strlen(body), body
);
close(client);
}
close(server_fd);
return 0;
}编译运行
1
2gcc -o server -s server.c
./servercurl命令发送pos请求调用接口
1
curl -X POST http://localhost:3000/echo -d "qi is testing"
2.创建post.html发送post请求调用REST API
post.html
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
<html>
<head>
<meta charset="UTF-8">
<title>REST API Test</title>
</head>
<body>
<h1>调用 C 语言 REST 服务</h1>
<button id="btnHello">GET /hello</button>
<pre id="helloResult"></pre>
<hr>
<input id="echoInput" placeholder="输入要发送的内容">
<button id="btnEcho">POST /echo</button>
<pre id="echoResult"></pre>
<script>
// 调用 GET /hello
document.getElementById('btnHello').onclick = async () => {
const res = await fetch('http://192.168.1.20:3000/hello');
const data = await res.json();
document.getElementById('helloResult').textContent =
JSON.stringify(data, null, 2);
};
// 调用 POST /echo
document.getElementById('btnEcho').onclick = async () => {
const text = document.getElementById('echoInput').value;
const res = await fetch('http://192.168.1.20:3000/echo', {
method: 'POST',
headers: { 'Content-Type': 'text/plain' },
body: text
});
const data = await res.json();
document.getElementById('echoResult').textContent =
JSON.stringify(data, null, 2);
};
</script>
</body>
</html>浏览器打开post.html
点击网页表单,在浏览器中查看结果
All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.
