http
ํ๋ฌํฐ์์ http ์์ฒญ (GET, POST, PUT, DELETE) ๋ฑ์ ์์ฒญ์ ํด์ฃผ๋ ํจํค์ง
install
dependencies:
http: ^1.2.2
Dart
๋ณต์ฌ
import
import 'package:http/http.dart' as http;
Dart
๋ณต์ฌ
์ฃผ์ ๋ด์ฉ
http ํจํค์ง ๊ธฐ๋ฅ์ ์๋ฒ ์ธก์ผ๋ก ๋ฐ์ดํฐ๋ฅผ ์์ฒญํ๊ณ ์๋ฒ๊ฐ ๋ณด๋ด์ค ๋ฐ์ดํฐ๋ฅผ ๋ณํํ์ฌ, ํด๋ผ์ด์ธํธ ์ธก ํ๋ฉด์์ ์ถ๋ ฅํ๊ธฐ ์ํด ์ฌ์ฉํฉ๋๋ค.
๊ทธ๋์,
์๋ฒ๋ก๋ถํฐ ์๋ต๋ฐ์ JSON ๋ฐ์ดํฐ๋ฅผ ๋ณํํ๊ธฐ ์ํ convert ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ ํจ๊ป ์ฌ์ฉํฉ๋๋ค.
convert
JSON ๋ฐ์ดํฐ ํ์ ๋ฑ ๋ค์ํ ๋ฐ์ดํฐ ํ์์ ์ฌ์ฉํ ์ ์๋ ๋ฐ์ดํฐ๋ก ๋ณํํ๋ ํจํค์ง
http ํจํค์ง ์ฃผ์ ๋ฉ์๋
๋ฉ์๋ | ์ค๋ช
| ์ฌ์ฉ ์์ |
get | ์ง์ ๋ URL๋ก๋ถํฐ ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ ธ์ต๋๋ค. | http.get(Uri.parse('https://example.com')); |
post | ์ง์ ๋ URL๋ก ๋ฐ์ดํฐ๋ฅผ ๋ณด๋
๋๋ค. | http.post(Uri.parse('https://example.com'), body: {'key': 'value'}); |
put | ์ง์ ๋ URL์ ๋ฐ์ดํฐ๋ฅผ ์
๋ฐ์ดํธํฉ๋๋ค. | http.put(Uri.parse('https://example.com'), body: {'key': 'value'}); |
delete | ์ง์ ๋ URL์ ๋ฐ์ดํฐ๋ฅผ ์ญ์ ํฉ๋๋ค. | http.delete(Uri.parse('https://example.com')); |
patch | ์ง์ ๋ URL์ ๋ถ๋ถ์ ์ผ๋ก ๋ฐ์ดํฐ๋ฅผ ์
๋ฐ์ดํธํฉ๋๋ค. | http.patch(Uri.parse('https://example.com'), body: {'key': 'value'}); |
head | ์ง์ ๋ URL์ ํค๋ ์ ๋ณด๋ฅผ ๊ฐ์ ธ์ต๋๋ค. | http.head(Uri.parse('https://example.com')); |
read | ์ง์ ๋ URL๋ก๋ถํฐ ์๋ต ๋ณธ๋ฌธ์ ๋ฌธ์์ด๋ก ๊ฐ์ ธ์ต๋๋ค. | http.read(Uri.parse('https://example.com')); |
readBytes | ์ง์ ๋ URL๋ก๋ถํฐ ์๋ต ๋ณธ๋ฌธ์ ๋ฐ์ดํธ ๋ฐฐ์ด๋ก ๊ฐ์ ธ์ต๋๋ค. | http.readBytes(Uri.parse('https://example.com')); |
multipartRequest | ๋ฉํฐํํธ ํผ ๋ฐ์ดํฐ์ ํจ๊ป HTTP ์์ฒญ์ ๋ณด๋
๋๋ค. | http.MultipartRequest('POST', Uri.parse('https://example.com')); |
http ์ค์ต
1.
GET ์์ฒญ
2.
POST ์์ฒญ
3.
PUT ์์ฒญ
4.
DELETE ์์ฒญ
GET ์์ฒญ
โข
๋ฆฌ์คํธ( [ { }, { }, { } ] ) ํํ๋ฅผ ์๋ต ๋ฐ์, ๋ณ๊ฒฝํ๋ ๊ฒฝ์ฐ
var url = "http://10.0.2.2:8080/board";
List<Board> list = [];
try {
var response = await http.get(Uri.parse(url));
print("::::: response - body :::::");
print(response.body);
// UTF-8 ๋์ฝ๋ฉ
var utf8Decoded = utf8.decode(response.bodyBytes);
// JSON ๋์ฝ๋ฉ
var boardList = jsonDecode(utf8Decoded);
for (var i = 0; i < boardList.length; i++) {
list.add(Board(
no: boardList[i]['no'],
title: boardList[i]['title'],
writer: boardList[i]['writer'],
content: boardList[i]['content'],
));
}
print(list);
} catch (e) {
print(e);
}
Dart
๋ณต์ฌ
โข
๊ฐ์ฒด { } ํํ๋ฅผ ์๋ต ๋ฐ์, ๋ณ๊ฒฝํ๋ ๊ฒฝ์ฐ
var url = "http://10.0.2.2:8080/board/$no";
try {
var response = await http.get(Uri.parse(url));
print("::::: response - body :::::");
print(response.body);
// UTF-8 ๋์ฝ๋ฉ
var utf8Decoded = utf8.decode(response.bodyBytes);
// JSON ๋์ฝ๋ฉ
var boardJson = jsonDecode(utf8Decoded);
print(boardJson);
return Board(
no: boardJson['no'],
title: boardJson['title'],
writer: boardJson['writer'],
content: boardJson['content'],
);
} catch (e) {
print(e);
}
Dart
๋ณต์ฌ
ํ๋ฌํฐ์์ ๋ก์ปฌ๋ก ํ
์คํธ ํ ๋๋ 10.0.2.2 ๋ก ์ง์ ํด์ผํ๋ค.
var url = 'http://10.0.2.2:8080/board';
Dart
๋ณต์ฌ
ํ์ฌ pc๋ฅผ ๊ฐ๋ฆฌํฌ ๋ ๋ก์ปฌ ํธ์คํธ๋ 127.0.0.1๋ก ์ง์ ์ ํ๋๋ฐ, ํ๋ฌํฐ์์๋ 10.0.2.2 ์
๋๋ค.
URL ๋ฅผ ์ธ์ฝ๋ฉ ํด์ ์ง์ ํด์ผํ๋ค.
var response = await http.get(Uri.parse(url));
Dart
๋ณต์ฌ
Uri.parse(url) ํจ์๋ฅผ ์ฌ์ฉํด์ URL ๋ฅผ ์ธ์ฝ๋ฉํ์ฌ ์ง์ ํด์ผํฉ๋๋ค.
์๋ต ๋ฐ์ดํฐ ํ์ธํ๊ธฐ
response.body
Dart
๋ณต์ฌ
์๋ต ๋ฐ์ดํฐ๋ฅผ ๋ณํํ๊ธฐ
var utf8Decoded = utf8.decode( response.bodyBytes );
Dart
๋ณต์ฌ
JSON ํ์์ ๋ฐ์ดํฐ๋ฅผ ๋ณํํ๊ธฐ
jsonDecode() ํจ์๋ convert ํจํค์ง์ ํฌํจ๋ ํจ์์
๋๋ค.
List<???> data = jsonDecode( utf8Decoded );
Dart
๋ณต์ฌ
โข
JSON
[
{ 'name' : 'ALOHA1', 'age' : '10' },
{ 'name' : 'ALOHA2', 'age' : '20' },
{ 'name' : 'ALOHA3', 'age' : '30' },
]
Dart
๋ณต์ฌ
์์ ๊ฐ์ JSON ํ์์ ๋ฐ์ดํฐ๋ฅผ Dart ์ List ๋ก ๋ณํํ์ฌ ์ฌ์ฉํฉ๋๋ค.
POST ์์ฒญ
var url = "http://10.0.2.2:8080/board";
try {
var response = await http.post(
Uri.parse(url),
headers: {"Content-Type": "application/json"},
body: jsonEncode({
'title': _titleController.text,
'writer': _writerController.text,
'content': _contentController.text,
}),
);
print("::::: response - body :::::");
print(response.body);
if (response.statusCode == 200 || response.statusCode == 201) {
// ์๋ต ์ฑ๊ณต ํ ์ฒ๋ฆฌ
}
catch (e) {
print(e);
}
Dart
๋ณต์ฌ
PUT ์์ฒญ
var url = "http://10.0.2.2:8080/board";
try {
var response = await http.put(
Uri.parse(url),
headers: {"Content-Type": "application/json"},
body: jsonEncode({
'no' : no,
'title': _titleController.text,
'writer': _writerController.text,
'content': _contentController.text,
}),
);
if (response.statusCode == 200 || response.statusCode == 204) {
// ์๋ต ์ฑ๊ณต ํ ์ฒ๋ฆฌ
}
} catch (e) {
print(e);
}
Dart
๋ณต์ฌ
DELETE ์์ฒญ
var url = "http://10.0.2.2:8080/board/$no";
try {
var response = await http.delete(Uri.parse(url));
print("::::: response - statusCode :::::");
print(response.statusCode);
if (response.statusCode == 200 || response.statusCode == 204) {
// ์ญ์ ์ฑ๊ณต ํ ์ฒ๋ฆฌ
}
} catch (e) {
print(e);
}
Dart
๋ณต์ฌ