i am trying to get a http post request to work on an esp32 where i try to send multipart data to an api.
currently this is how it looks, but i just cannot get it to run properly. it does not send the data correctly or in other words, it only (i think) sends the first part):
<code>void sendPhoto(const uint8_t *data, size_t len) {
WiFiClient client;
HTTPClient http;
unsigned long startTime = millis(); // Start time for measuring response time
if (http.begin(client, api_url)) {
http.addHeader("x-api-key", api_key);
http.addHeader("Content-Type", "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW");
String body = "------WebKitFormBoundary7MA4YWxkTrZu0gWrn";
body += "Content-Disposition: form-data; name="file"; filename="image.jpg"rn";
body += "Content-Type: image/jpegrnrn";
int httpResponseCode = http.sendRequest("POST", body + String((char*)data, len) + "rn------WebKitFormBoundary7MA4YWxkTrZu0gW--");
unsigned long responseTime = millis() - startTime; // Calculate response time
if (httpResponseCode > 0) {
String response = http.getString();
Serial.println("Response Code: " + String(httpResponseCode));
Serial.println("Response: " + response);
Serial.println("Response Time: " + String(responseTime) + " ms");
} else {
Serial.println("Error: " + String(httpResponseCode));
}
http.end();
} else {
Serial.println("Unable to connect to server");
}
}
</code>
<code>void sendPhoto(const uint8_t *data, size_t len) {
WiFiClient client;
HTTPClient http;
unsigned long startTime = millis(); // Start time for measuring response time
if (http.begin(client, api_url)) {
http.addHeader("x-api-key", api_key);
http.addHeader("Content-Type", "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW");
String body = "------WebKitFormBoundary7MA4YWxkTrZu0gWrn";
body += "Content-Disposition: form-data; name="file"; filename="image.jpg"rn";
body += "Content-Type: image/jpegrnrn";
int httpResponseCode = http.sendRequest("POST", body + String((char*)data, len) + "rn------WebKitFormBoundary7MA4YWxkTrZu0gW--");
unsigned long responseTime = millis() - startTime; // Calculate response time
if (httpResponseCode > 0) {
String response = http.getString();
Serial.println("Response Code: " + String(httpResponseCode));
Serial.println("Response: " + response);
Serial.println("Response Time: " + String(responseTime) + " ms");
} else {
Serial.println("Error: " + String(httpResponseCode));
}
http.end();
} else {
Serial.println("Unable to connect to server");
}
}
</code>
void sendPhoto(const uint8_t *data, size_t len) {
WiFiClient client;
HTTPClient http;
unsigned long startTime = millis(); // Start time for measuring response time
if (http.begin(client, api_url)) {
http.addHeader("x-api-key", api_key);
http.addHeader("Content-Type", "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW");
String body = "------WebKitFormBoundary7MA4YWxkTrZu0gWrn";
body += "Content-Disposition: form-data; name="file"; filename="image.jpg"rn";
body += "Content-Type: image/jpegrnrn";
int httpResponseCode = http.sendRequest("POST", body + String((char*)data, len) + "rn------WebKitFormBoundary7MA4YWxkTrZu0gW--");
unsigned long responseTime = millis() - startTime; // Calculate response time
if (httpResponseCode > 0) {
String response = http.getString();
Serial.println("Response Code: " + String(httpResponseCode));
Serial.println("Response: " + response);
Serial.println("Response Time: " + String(responseTime) + " ms");
} else {
Serial.println("Error: " + String(httpResponseCode));
}
http.end();
} else {
Serial.println("Unable to connect to server");
}
}
if anyone could share an idea what the problem ist id be very happy 😉
thanks a lot!!!