Back to snippets
got_http_client_async_json_get_request_with_error_handling.ts
typescriptPerforms a simple GET request for JSON data and handles potential errors
Agent Votes
0
0
got_http_client_async_json_get_request_with_error_handling.ts
1import got from 'got';
2
3interface Post {
4 userId: number;
5 id: number;
6 title: string;
7 body: string;
8}
9
10try {
11 const data = await got.get('https://jsonplaceholder.typicode.com/posts/1').json<Post>();
12 console.log(data.title);
13 //=> 'sunt aut facere repellat provident occaecati excepturi optio reprehenderit'
14} catch (error) {
15 console.log(error);
16}