完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
电子发烧友论坛|
我正在尝试通过 GET 和 POST 与服务器通信。GET 我已经可以通过接收一个 Json 文件并从该文件中获取我需要的值来完成它。现在我需要通过 POST 发送这些变量:
grant_type: password client_id: 2 client_secret: lIFOudKXgfP7hmzgpevs3lFNtQTJAgRvXC6OU0wQ 用户名:ADM123 密码:aaaaaa 我正在实施的代码如下: 代码:全选#include #include #include // WiFi Parameters const char* ssid = "BALTRAC"; //Nombre de la red Wi-Fi const char* password = "baltrac1140824610"; //Contraseña de la red Wi-Fi. void setup() { Serial.begin(115200); WiFi.begin(ssid, password); //Inicia la conexión con la red Wi-Fi. while (WiFi.status() != WL_CONNECTED) { delay(1000); Serial.println("Connecting..."); } } void loop() { // Check WiFi Status if (WiFi.status() == WL_CONNECTED) { //Si hay acceso a internet. HTTPClient http; //Objeto de clase HTTPClient //http.begin("http://jsonplaceholder.typicode.com/todos/1"); //Conecta con servidor. http.begin("http://18.223.239.136/api/v1/users/grant_client"); int httpCode = http.GET(); //Realiza una petición al servidor. if (httpCode > 0) { //Si hay algo que recibir del servidor. const size_t bufferSize = JSON_OBJECT_SIZE(2) + JSON_OBJECT_SIZE(3) + JSON_OBJECT_SIZE(5) + JSON_OBJECT_SIZE(8) + 370; DynamicJsonBuffer jsonBuffer(bufferSize); JsonObject& root = jsonBuffer.parseObject(http.getString()); // Parameters int id = root["id"]; // 1 const char * name = root ["secret"]; // "Leanne Graham" //const char * username = root ["username"]; // "Bret" //const char * email = root ["email"]; // "Sincero@april.biz" Serial.print("id: "); Serial.println(id); Serial.print("Secret: "); Serial.println(name); // Serial.print("Username:"); // Serial.println(username); // Serial.print("Email:"); // Serial.println(email); } http.end(); //Close connection /* grant_type:password client_id:{{client_id}} client_secret:{{client_secret}} username:ADM123 password:aaaaaa */ //HTTPClient http; String grant_type, client_id ,client_secret, username, password, message; grant_type = "password"; client_id = "2"; client_secret = "lIFOudKXgfP7hmzgpevs3lFNtQTJAgRvXC6OU0wQ"; username = "ADM123"; password = "aaaaaa"; message = "grant_type:" + grant_type + "&" + "client_id:" + client_id + "&" + "client_secret:" + client_secret + "&" + "username:" + username + "&" + "password:" + password; Serial.println(message); http.begin("http://18.223.239.136/oauth/token"); http.addHeader("Content-Type", "application / x-www-form-urlencoded"); //http.addHeader("Content-Type", "text/plain"); httpCode = http.POST(message); if(httpCode > 0) { Serial.printf("[HTTP] POST... code: %dn", httpCode); if(httpCode == HTTP_CODE_OK) { String payload = http.getString(); Serial.println(payload); } } else { Serial.printf("[HTTP] POST... failed, error: %sn", http.errorToString(httpCode).c_str()); } http.end(); } // Delay delay(10000); } |
|
相关推荐
|
|
只有小组成员才能发言,加入小组>>
470 浏览 0 评论
991 浏览 0 评论
1709 浏览 0 评论
请问一下我想用ESP8685直接替换ESP8266而不用写程序,可以让ESP8685直接通过之前ESP8266的外挂的flash运行程序吗
1368 浏览 1 评论
1254 浏览 1 评论
为blufi_device设置自定义名称,但是无法修改,为什么?
1288浏览 4评论
请问ESP32-S2-WROOM怎么获得ESP32-S2外接FLASH的唯一序列号?
943浏览 3评论
2386浏览 3评论
ESP-IDF的VScode插件的build按钮点击会报错的原因?
2573浏览 3评论
ESP-Jumpstart例程中第5个工程:5_cloud连接报错是哪里的问题?
1071浏览 2评论
/9
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2025-12-13 19:12 , Processed in 1.198653 second(s), Total 39, Slave 32 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191

淘帖
372
