To demonstrate how to utilize curl
in a real-world scenario, here's a quick example of interacting with a social network platform. Mastodon is an open-source, federated social network and microblogging platform, and features a rich API to read, write, and manage your account. Other social networks may have similar features, and while the exact API commands differ from site to site, Mastodon provides a reasonable example of the process, and in just three simple steps.
If you want to try this process yourself, you must sign up for a Mastodon account (and if you do, be sure to follow the opensource.com bot to see a shell and curl
-based bot in action).
[ Read me first: Using curl to interact with an API. ]
Create an app
After creating an account on a Mastodon server, you must create an app hosted by your user. This is done through the website, in the Development panel of your user profile.
Get your API key
After creating a new app, click the app name to see its private credentials. In this case, your credentials include a client key, a client secret, and an authentication token.
This is common regardless of what API you're using. There may only be one API key, or there may be several components, but the point is that you've generated a unique and secret hash that you can use in the program or script that you're using to call on the API.
Learn the API
Learning to talk to an API is exactly the same as learning a new Linux command, a new shell, or a new programming language. You need to know what words are used to structure a command or query, you need to understand the syntax, and you need to know what external information you're required to provide.
Generally, if a service provides an API, it also provides (up-to-date, ideally) documentation on how to use that API. In the case of Mastodon, there's very good documentation on how to interact with your account through the API so that you can learn the language with minimal pain.
Make a post
According to Mastodon's API documentation, you can test your credentials by sending your authorization token, preceded by the string Bearer
, as a special header to the API endpoint.
https://mastodon.example/api/v1/accounts/verify_credentials
For example:
$ curl --header 'Authorization: Bearer K90yYtm_1fMxH775bw31-775pnPDZvmQWSJzg' \
https://mastodon.example/api/v1/accounts/verify_credentials
In return, you get a JSON dump of your user profile.
Other API interactions require more information. For example, to post a status to your account, you must also provide form data for the status
field:
$ curl --header 'Authorization: Bearer K90yYtm_1fMxH775bw31-775pnPDZvmQWSJzg' \
--form "status=Test post from curl" \
"https://mastodon.xyz/api/v1/statuses"
Using curl for remote interactions
Coupled with the right API, curl
is almost a network programming language. It's easy to use as the courier for your commands, and it has troubleshooting tools built-in. Use curl
the next time you need to test or interact with an API.
[ Want to become a curl power user? Check out the curl command cheat sheet. ]
저자 소개
Seth Kenlon is a Linux geek, open source enthusiast, free culture advocate, and tabletop gamer. Between gigs in the film industry and the tech industry (not necessarily exclusive of one another), he likes to design games and hack on code (also not necessarily exclusive of one another).
채널별 검색
오토메이션
기술, 팀, 인프라를 위한 IT 자동화 최신 동향
인공지능
고객이 어디서나 AI 워크로드를 실행할 수 있도록 지원하는 플랫폼 업데이트
오픈 하이브리드 클라우드
하이브리드 클라우드로 더욱 유연한 미래를 구축하는 방법을 알아보세요
보안
환경과 기술 전반에 걸쳐 리스크를 감소하는 방법에 대한 최신 정보
엣지 컴퓨팅
엣지에서의 운영을 단순화하는 플랫폼 업데이트
인프라
세계적으로 인정받은 기업용 Linux 플랫폼에 대한 최신 정보
애플리케이션
복잡한 애플리케이션에 대한 솔루션 더 보기
오리지널 쇼
엔터프라이즈 기술 분야의 제작자와 리더가 전하는 흥미로운 스토리
제품
- Red Hat Enterprise Linux
- Red Hat OpenShift Enterprise
- Red Hat Ansible Automation Platform
- 클라우드 서비스
- 모든 제품 보기
툴
체험, 구매 & 영업
커뮤니케이션
Red Hat 소개
Red Hat은 Linux, 클라우드, 컨테이너, 쿠버네티스 등을 포함한 글로벌 엔터프라이즈 오픈소스 솔루션 공급업체입니다. Red Hat은 코어 데이터센터에서 네트워크 엣지에 이르기까지 다양한 플랫폼과 환경에서 기업의 업무 편의성을 높여 주는 강화된 기능의 솔루션을 제공합니다.