Search

http

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
๋ณต์‚ฌ