Dart 사용자를 위한 URL 설명

HyunHo Shin·2023년 6월 4일
0

이번 글에서는 http api를 이용하여 데이터를 주고받을 때 꼭 알아야 하는 url의 구조에 대해서 알아보도록 하겠습니다.

URL이란?

정말 간단하게 얘기해서 특정 사이트에 접속하기 위한 문자열 주소를 말합니다.

URL의 구조

주소창 주소 예시
https://opentdb.com:443/api.php?amount=10&category=11&difficulty=hard&type=multiple

개념적 구조
scheme://host:port/path?query#fragment

  1. Schema: 해당 주소에 접속할 때 사용하는 protocol
  2. host: 웹사이트의 domain name
  3. port: 네트워크 서비스를 식별하는 번호
  4. path: 서버내부의 위치
  5. query: 네트워크에 전달한 세부 Map 형식의 세부정보
  6. fragement: 리소스의 특정 위치를 나타내기 위한 식별자

예시

위의 정보를 바탕으로 다음 URL을 한번 분석해 보겠습니다.

https://opentdb.com:443/api.php?amount=10&category=11&difficulty=hard&type=multiple
  • Schema: https
  • host: opentdb.com
  • port: 443
  • path: api.php
  • qurey: amount=10&category=11&difficulty=hard&type=multiple

Dart 적용 예시

import 'package:http/http.dart' as http;
var url = Uri.https('opentdb.com', 'api.php?', 
					{'amount':10, 'category':11, 'difficulty: 'hard', type: 'multiple'});
  • Uri.http의 argument로 host, path, query를 기입하면서 해당 url의 자료를 get할 수 있습니다.
profile
관심사가 다양한 사람

0개의 댓글