0Pricing
Arduino & IoT Academy · レッスン

APIからGETデータを取得する

HTTPリクエストを送り、レスポンスを読み取ります。

「APIからGETデータを取得する」はCoddyKit上の無料Arduino & IoT Academyレッスンです。 これはレッスン1/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはArduino & IoT Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Arduino & IoT Academyコースには全4レッスンが含まれています。

このレッスンの一部はまだ翻訳されておらず、英語で表示されています。

The Web Is Just Requests

Your board can pull data from the internet the same way a browser does: by making an HTTP request to a URL. 🌐

What GET Means

A GET request asks a server, please send me this data. It only reads, it never changes anything on the other side.

You Need a Connection First

Before any request, your ESP must already be on WiFi. Without a network connection there is nowhere to send the request.

Include HTTPClient

The HTTPClient library gives you simple functions to make requests. Include it at the top of your sketch.

#include <HTTPClient.h>

Create the Client

Make an HTTPClient object, then point it at your target URL with begin(). This sets up the request you are about to send.

HTTPClient http;
http.begin("http://api.example.com/data");

Send the GET

Call GET() to fire the request. It returns an HTTP status code that tells you whether the server liked your request.

int code = http.GET();

Check the Status Code

A code of 200 means success. Always check it before trusting the data, since 404 or 500 means something went wrong.

if (code == 200) {
  // good response
}

Read the Response Body

When the status is good, grab the actual data with getString(). This is the text the server sent back to you.

String body = http.getString();

Always Free the Connection

Call end() when you are done. This closes the connection and frees memory so the next request starts clean.

http.end();

Negative Codes Are Local

A negative status code does not come from the server. It means the request failed on your side, often no WiFi or a bad URL.

Do Not Hammer the Server

Add a delay between requests so you do not flood the API. Most services limit how often you may ask for data.

Quick Check

Your code received an HTTP status of 200. What does that tell you?

Recap

You connected, called GET(), checked for 200, read the body with getString(), and closed with end(). That is a full read from the web. 🎉

よくある質問

「APIからGETデータを取得する」レッスンは無料ですか?

はい。「APIからGETデータを取得する」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Arduino & IoT Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Arduino & IoT Academyコースには全4レッスンが含まれています。

「APIからGETデータを取得する」で何を学びますか?

HTTPリクエストを送り、レスポンスを読み取ります。 ブラウザで直接実行するハンズオンコードでArduino & IoT Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

Arduino & IoT Academyを始めるのに経験は必要ですか?

事前経験は必要ありません。CoddyKitのArduino & IoT Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン1/4です。

「APIからGETデータを取得する」レッスンにはどのくらい時間がかかりますか?

ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。

このArduino & IoT Academyレッスンでコードを書いて実行できますか?

はい。すべてのArduino & IoT Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。

このコースのすべてのレッスン

  1. APIからGETデータを取得する
  2. センサー値をPOSTする
  3. JSONレスポンスを解析する
  4. HTTPSを安全に使う
← Arduino & IoT Academyに戻る