본문 바로가기
Application/Flutter

[Cookbook] Authenticated Request 보내는 방법

by 비나래 2022. 7. 25.

[목적]

대부분의 web service에서 authorization을 부여해야 함.

HTTP Header에서 Authorization을 주는 방법 알기

 

[방법]

http package에서 header에 추가하는 손쉬운 방법을 제공함

 

[예시]

Future<Album> fetchAlbum() async {
	final response = await http.get(
    	Uri.parse('https://jsonplaceholder.typicode.com/albums/1'),
    	headers: {
        	httpHeaders.authorizationHeader: 'Basic your_api_token_here',
        },
	);
	final responseJson = jsonDecode(response.body);
	return Album.fromJson(responseJson);
}