esp-request, 不再支持这里项目,请使用

分享于 

5分钟阅读

GitHub

  繁體 雙語
ESP32 HTTP Client library
  • 源代码名称:esp-request
  • 源代码网址:http://www.github.com/tuanpmt/esp-request
  • esp-request源代码文档
  • esp-request源代码下载
  • Git URL:
    git://www.github.com/tuanpmt/esp-request.git
    Git Clone代码到本地:
    git clone http://www.github.com/tuanpmt/esp-request
    Subversion代码到本地:
    $ svn co --depth empty http://www.github.com/tuanpmt/esp-request
    Checked out revision 1.
    $ cd repo
    $ svn up trunk
    
    面向ESP32的轻量级HTTP客户端

    示例

    不再支持这里项目,请使用

    https://github.com/espressif/esp-idf/tree/master/components/esp_http_client

    intdownload_callback(request_t *req, char *data, int len)
    {
     req_list_t *found = req->response->header;
     while(found->next!= NULL) {
     found = found->next;
     ESP_LOGI(TAG,"Response header %s:%s", (char*)found->key, (char*)found->value);
     }
     //or  found = req_list_get_key(req->response->header, "Content-Length");
     if(found) {
     ESP_LOGI(TAG,"Get header %s:%s", (char*)found->key, (char*)found->value);
     }
     ESP_LOGI(TAG,"%s", data);
     return0;
    }staticvoidrequest_task(void *pvParameters)
    {
     request_t *req;
     int status;
     xEventGroupWaitBits(wifi_event_group, CONNECTED_BIT, false, true, portMAX_DELAY);
     ESP_LOGI(TAG, "Connected to AP, freemem=%d",esp_get_free_heap_size());
     // vTaskDelay(1000/portTICK_RATE_MS); req = req_new("http://httpbin.org/post"); 
     //or//request *req = req_new("https://google.com");//for SSLreq_setopt(req, REQ_SET_METHOD, "POST");
     req_setopt(req, REQ_SET_POSTFIELDS, "test=data&test2=data2");
     req_setopt(req, REQ_FUNC_DOWNLOAD_CB, download_callback);
     status = req_perform(req);
     req_clean(req);
     vTaskDelete(NULL);
    }

    web socket

    intwebsocket_cb(request_t *req, int status, void *buffer, int len)
    {
     switch(status) {
     case WS_CONNECTED:
     ESP_LOGI(TAG, "websocket connected");
     req_write(req, "hello world", 11);
     break;
     case WS_DATA:
     ((char*)buffer)[len] = 0;
     ESP_LOGI(TAG, "websocket data = %s", (char*)buffer);
     req_close(req);
     break;
     case WS_DISCONNECTED:
     ESP_LOGI(TAG, "websocket disconnected");
     req_clean(req);
     req = NULL;
     break;
     }
     return0;
    }voidapp()
    {
     request_t *req = req_new("ws://echo.websocket.org"); // or wss://echo.websocket.orgreq_setopt(req, REQ_FUNC_WEBSOCKET, websocket_cb);
     req_perform(req);
    }

    用法

    API

    函数

    • req_new
    • req_setopt
    • req_clean

    用于 req_setopt- 选项

    • REQ_SET_METHOD - req_setopt(req, REQ_SET_METHOD,"GET");//or POST/PUT/DELETE
    • REQ_SET_HEADER - req_setopt(req, REQ_SET_HEADER,"HeaderKey: HeaderValue");
    • REQ_SET_HOST - req_setopt(req, REQ_SET_HOST,"google.com");//or 192.168.0.1
    • REQ_SET_PORT - req_setopt(req, REQ_SET_PORT,"80");//must be string
    • REQ_SET_PATH - req_setopt(req, REQ_SET_PATH,"/path");
    • REQ_SET_SECURITY
    • REQ_SET_URI - req_setopt(req, REQ_SET_URI,"http://uri.com");//will replace host, port, path, security and Auth if present
    • REQ_SET_DATAFIELDS
    • REQ_SET_UPLOAD_LEN - 现在不影响
    • REQ_FUNC_DOWNLOAD_CB - req_setopt(req, REQ_FUNC_DOWNLOAD_CB, download_callback);
    • REQ_FUNC_UPLOAD_CB
    • REQ_FUNC_WEBSOCKET
    • REQ_REDIRECT_FOLLOW - req_setopt(req, REQ_REDIRECT_FOLLOW,"true");//or"false"

    URI格式

    待办事项

    • 支持URI分析器
    • 跟随重定向
    • 支持 SSL
    • 支持设置字段字段( 简单)
    • 支持 web socket & web socket安全
    • 支持基本认证
    • 支持上传多部分
    • 支持 Cookie

    已知问题

    • Uri解析需要更多工作

    作者


    proj  USE  SUP  THIS  long  
    相关文章