Explorar o código

requests::uploadFile, requests::uploadFormFile

ithewei %!s(int64=4) %!d(string=hai) anos
pai
achega
5decc083d0
Modificáronse 4 ficheiros con 35 adicións e 3 borrados
  1. 2 2
      examples/httpd/handler.h
  2. 1 1
      http/HttpMessage.h
  3. 24 0
      http/client/requests.h
  4. 8 0
      http/http_content.h

+ 2 - 2
examples/httpd/handler.h

@@ -191,8 +191,8 @@ public:
         resp->form["int"] = 123;
         resp->form["int"] = 123;
         resp->form["float"] = 3.14;
         resp->form["float"] = 3.14;
         resp->form["string"] = "hello";
         resp->form["string"] = "hello";
-        // resp->form["file"] = FormData(NULL, "test.jpg");
-        // resp->UploadFormFile("file", "test.jpg");
+        // resp->form["file"] = FormFile("test.jpg");
+        // resp->FormFile("file", "test.jpg");
         return 200;
         return 200;
     }
     }
 
 

+ 1 - 1
http/HttpMessage.h

@@ -146,7 +146,7 @@ public:
         return 200;
         return 200;
     }
     }
 
 
-    void UploadFormFile(const char* name, const char* filepath) {
+    void FormFile(const char* name, const char* filepath) {
         content_type = MULTIPART_FORM_DATA;
         content_type = MULTIPART_FORM_DATA;
         form[name] = FormData(NULL, filepath);
         form[name] = FormData(NULL, filepath);
     }
     }

+ 24 - 0
http/client/requests.h

@@ -61,6 +61,30 @@ HV_INLINE Response request(http_method method, const char* url, const http_body&
     return request(req);
     return request(req);
 }
 }
 
 
+HV_INLINE Response uploadFile(http_method method, const char* url, const char* filepath, const http_headers& headers = DefaultHeaders) {
+    Request req(new HttpRequest);
+    req->method = method;
+    req->url = url;
+    if (req->File(filepath) != 200) return NULL;
+    if (&headers != &DefaultHeaders) {
+        req->headers = headers;
+    }
+    return request(req);
+}
+
+#ifndef WITHOUT_HTTP_CONTENT
+HV_INLINE Response uploadFormFile(http_method method, const char* url, const char* name, const char* filepath, const http_headers& headers = DefaultHeaders) {
+    Request req(new HttpRequest);
+    req->method = method;
+    req->url = url;
+    req->FormFile(name, filepath);
+    if (&headers != &DefaultHeaders) {
+        req->headers = headers;
+    }
+    return request(req);
+}
+#endif
+
 HV_INLINE Response head(const char* url, const http_headers& headers = DefaultHeaders) {
 HV_INLINE Response head(const char* url, const http_headers& headers = DefaultHeaders) {
     return request(HTTP_HEAD, url, NoBody, headers);
     return request(HTTP_HEAD, url, NoBody, headers);
 }
 }

+ 8 - 0
http/http_content.h

@@ -43,6 +43,14 @@ struct FormData {
         content = hv::to_string(num);
         content = hv::to_string(num);
     }
     }
 };
 };
+// FormFile
+struct FormFile : public FormData {
+    FormFile(const char* filename = NULL) {
+        if (filename) {
+            this->filename = filename;
+        }
+    }
+};
 
 
 // MultiPart
 // MultiPart
 // name => FormData
 // name => FormData