#include <WiFi.h>
#include <HTTPClient.h>
// WiFi 設定
//const char *ssid = "Facon";
//const char *pswd = "84488125";
const char *ssid = "Wokwi-GUEST";
const char *pswd = "";
// REST API Server 設定
String serverAddress = "https://petstore.swagger.io/v2/pet/";
void setup() {
// 連接 WiFi
WiFi.begin(ssid, pswd);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("WiFi connected!");
// 初始化序列埠
Serial.begin(115200);
}
String input_str;
void loop() {
Serial.println("----------------------------------------");
Serial.println("輸入模式:1.GET 2.POST 3.DELETE");
while (!Serial.available()) {} //等待串列輸入資料才繼續往下
if (Serial.available()) { // 檢查串列埠上是否有可用的資料
//這是字串接收的格式
int mode_select = Serial.readString().toInt(); // 讀取接收到的資料
String input_str;//輸入ID用
String input_str1;//POST模式輸入Name用
switch (mode_select) {
case 1:
Serial.println("請輸入GET ID");
while (!Serial.available()) {} //如未輸入時停留在此等待輸入
input_str = Serial.readString();//輸入的文字因為會帶有換行
input_str.trim();//這行用意為清除換行
Serial.println("GET ID=" + input_str);
REST_API_GET(input_str);
break;
case 2:
Serial.println("請輸入POST ID");
while (!Serial.available()) {} //如未輸入時停留在此等待輸入
input_str = Serial.readString();//輸入的文字因為會帶有換行
input_str.trim();//這行用意為清除換行
Serial.println("請輸入POST Name");
while (!Serial.available()) {} //如未輸入時停留在此等待輸入
input_str1 = Serial.readString();//輸入的文字因為會帶有換行
input_str1.trim();//這行用意為清除換行
Serial.println("POST ID=" + input_str);
Serial.println("POST Name=" + input_str1);
REST_API_POST(input_str,input_str1);
break;
case 3:
Serial.println("請輸入DELETE ID");
while (!Serial.available()) {} //如未輸入時停留在此等待輸入
input_str = Serial.readString();//輸入的文字因為會帶有換行
input_str.trim();//這行用意為清除換行
Serial.println("DELETE ID=" + input_str);
REST_API_DELETE(input_str);
break;
case 4:
Serial.println("號碼錯誤");
break;
default:
Serial.println("號碼錯誤");
break;
}
}
}
void REST_API_GET(String GET_ID) {
// 設定 HTTPClient
HTTPClient http;
// 發送 GET 請求
String getRequestURL = serverAddress + GET_ID;
http.begin(getRequestURL);
int getHttpResponseCode = http.GET();
// 處理 GET 回應
if (getHttpResponseCode > 0) {
Serial.print("HTTP Response code: ");
Serial.println(getHttpResponseCode);
String getResponsePayload = http.getString();
Serial.println("GET Response: " + getResponsePayload);
} else {
Serial.println("Error getting response");
}
// 釋放 HTTP 連線資源
http.end();
}
void REST_API_POST(String POST_ID,String POST_Name) {
// 設定 HTTPClient
HTTPClient http;
// 發送 POST 請求
String postRequestURL = serverAddress;
http.begin(postRequestURL);
http.addHeader("Content-Type", "application/json");
String postRequestBody = "{\"id\": " + POST_ID + "," +"\"name\": " + "\"" + POST_Name + "\"" + "}";//特殊符號前要加\,實際string內容為{"id": 9999,"name": "doggie"}
//Serial.println(postRequestBody);//列出POST出的網頁碼
int postHttpResponseCode = http.POST(postRequestBody);
// 處理 POST 回應
if (postHttpResponseCode > 0) {
Serial.print("HTTP Response code: ");
Serial.println(postHttpResponseCode);
String postResponsePayload = http.getString();
Serial.println("POST Response: " + postResponsePayload);
} else {
Serial.println("Error getting response");
}
// 釋放 HTTP 連線資源
http.end();
}
void REST_API_DELETE(String DELETE_ID) {
// 設定 HTTPClient
HTTPClient http;
String DELETERequestURL = serverAddress + DELETE_ID;
http.begin(DELETERequestURL);
http.addHeader("Content-Type", "application/json");
int httpResponseCode = http.sendRequest("DELETE");
Serial.print("HTTP Response code: ");
Serial.println(httpResponseCode);
// 釋放 HTTP 連線資源
http.end();
}