Upload Files to REST Web API

User avatar
xvinny
Posts: 12
Joined: Wed Aug 16, 2017 1:15 pm
Location: Curitiba, Brazil

Upload Files to REST Web API

Postby xvinny » Fri May 31, 2019 3:27 pm

Hi all,

I developed an Web API which exposes some GET and POST methods and I'm using ESP32 to access it.
One of the methods is a POST that allows to upload files. This method has two parameters:
1. serial: the serial number of the device
2. file: the file

I tested the API with Postman and it is working. Now, I'm trying to develop a code in ESP32 that upload files stored in the SD Card, but I don't have idea of how to do it.

The POST code snippet generated by Postman is the following:

Code: Select all

POST /api/Logs/UploadFiles?serialNumber=000001 HTTP/1.1
Host: mytesthost.azurewebsites.net
User-Agent: PostmanRuntime/7.13.0
Accept: */*
Cache-Control: no-cache
Postman-Token: 65898e8d-76fe-44ba-bfc1-183c95dd84d0,84a87f98-2d89-4744-88e7-7ad806c540cc
Host: mytesthost.azurewebsites.net
cookie: ARRAffinity=b6c882a5e013db277ef434fb044e8255f520bbdc70059876373bab098d534452
accept-encoding: gzip, deflate
content-type: multipart/form-data; boundary=--------------------------056345172925097284856335
content-length: 5091489
Connection: keep-alive
cache-control: no-cache
Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW

Content-Disposition: form-data; name="file"; filename="/C:/Temp/tests/LOG/1.txt
I believe that the code should looks something like this... But I don't know:
- which headers must be added?
- how I send the file?

Code: Select all

HTTPClient http;

    String URL = "https://mytesthost.azurewebsites.net/api/Logs/UploadFiles?serialNumber=" + String(ConfigurationManager.Device.SerialNumber);
    ESP_LOGD(TAG_SERVER, "[HTTP] Starting upload: %s", URL.c_str());    
    
    if (http.begin(URL))
    {
        String payload = "";

        http.addHeader("accept", "text/plain");
        http.addHeader("Content-Type", "multipart/form-data");

        int httpCode = http.POST(payload);
    }
I didn't find any code or example matching similar needs. Someone could help me?
___________________________________________________________________________________________________________________________
Tell me if you knows how to use the Programmer module of Windows Calc and I'm gonna say who you are.

User avatar
xvinny
Posts: 12
Joined: Wed Aug 16, 2017 1:15 pm
Location: Curitiba, Brazil

Re: Upload Files to REST Web API

Postby xvinny » Tue Jun 11, 2019 2:40 pm

Anyone can help :?:
___________________________________________________________________________________________________________________________
Tell me if you knows how to use the Programmer module of Windows Calc and I'm gonna say who you are.

Who is online

Users browsing this forum: No registered users and 68 guests