Anyone who can and wants to help by answering with examples of codes that really work, I really appreciate it:
I experimented again with an ESP32-cam. This time I intend to send a face photo to an API.
This API has other functions (GET and DELETE) that I can already do. But POST, which sends the photo, I can't.
I've googled dozens of examples.
This API (and many others) show code samples in all programming languages EXCEPT C++
I didn't want this post to get so long but I don't see any other way.
Next, I will show code examples in at least three different languages, but which, in my opinion, are more similar to C++
PHP HTTP V1.0
<?php
$request = new HttpRequest();
$request->setUrl('https://face-recognition18.p.rapidapi.com/register_face');
$request->setMethod(HTTP_METH_POST);
$request->setHeaders([
'content-type' => 'multipart/form-data; boundary=---011000010111000001101001',
'x-face-uid' => 'rostomulher',
'X-RapidAPI-Key' => 'f24a8bec96msh336f26930e2c623p10fc12jsn',
'X-RapidAPI-Host' => 'face-recognition18.p.rapidapi.com'
]);
$request->setBody('-----011000010111000001101001
Content-Disposition: form-data; name="face_image"
-----011000010111000001101001--
');
try {
$response = $request->send();
echo $response->getBody();
} catch (HttpException $ex) {
echo $ex;
}
HTTP 1.1
POST /register_face HTTP/1.1
Content-Type: multipart/form-data; boundary=---011000010111000001101001
X-Face-Uid: rostomulher
X-Rapidapi-Key: f24a8bec96msh336f26930e2c623p10fc12jsn
X-Rapidapi-Host: face-recognition18.p.rapidapi.com
Host: face-recognition18.p.rapidapi.com
Content-Length: 121
-----011000010111000001101001
Content-Disposition: form-data; name="face_image"
-----011000010111000001101001--
C# HttpClient
var client = new HttpClient();
var request = new HttpRequestMessage
{
Method = HttpMethod.Post,
RequestUri = new Uri("https://face-recognition18.p.rapidapi.com/register_face"),
Headers =
{
{ "x-face-uid", "rostomulher" },
{ "X-RapidAPI-Key", "f24a8bec96msh336f26930e2c623p10fc12jsn" },
{ "X-RapidAPI-Host", "face-recognition18.p.rapidapi.com" },
},
Content = new MultipartFormDataContent
{
new StringContent("")
{
Headers =
{
ContentType = new MediaTypeHeaderValue("application/octet-stream"),
ContentDisposition = new ContentDispositionHeaderValue("form-data")
{
Name = "face_image",
FileName = "RostoMulher.jpg",
}
}
},
},
};
using (var response = await client.SendAsync(request))
{
response.EnsureSuccessStatusCode();
var body = await response.Content.ReadAsStringAsync();
Console.WriteLine(body);
}
The relevant part of the code I'm using to try to send the camera photo:
if (client.connect(serverName.c_str(), serverPort)) {
Serial.println("Connection successful!");
String head = "---011000010111000001101001\r\nContent-Disposition: form-data; Name=\"face_image\" \r\n\r\n";
// String head = "---011000010111000001101001\r\nContent-Disposition: form-data; Name=\"face_image\"; FileName=\"pessoaA.jpg\"\r\nContent-Type: image/jpeg\r\n\r\n";
String tail = "\r\n---011000010111000001101001\r\n";
uint16_t imageLen = fb->len;
uint16_t extraLen = head.length() + tail.length();
uint16_t totalLen = imageLen + extraLen;
client.println("POST " + serverPath + " HTTP/1.1");
client.println("Host: " + serverName);
client.println("Content-Type: multipart/form-data; boundary=---011000010111000001101001");
client.println("x-face-uid:pessoaA");
client.println("X-RapidAPI-Key:f24a8bec96msh336f26930e2c623p10fc12jsn");
client.println("Content-Length: " + String(totalLen));
client.println();
client.print(head);
Serial.println(head);
uint8_t *fbBuf = fb->buf;
size_t fbLen = fb->len;
for (size_t n=0; n<fbLen; n=n+1024) {
if (n+1024 < fbLen) {
client.write(fbBuf, 1024);
fbBuf += 1024;
}
else if (fbLen%1024>0) {
size_t remainder = fbLen%1024;
client.write(fbBuf, remainder);
}
}
client.print(tail);
esp_camera_fb_return(fb);
And the error I see on the serial monitor:
Connecting to server: face-recognition18.p.rapidapi.com
Connection successful!
---011000010111000001101001
Content-Disposition: form-data; Name="face_image"
...............
getBody é:
{"detail":[{"loc":["body","face_image"],"msg":"field required","type":"value_error.missing"}]}
I don't understand down there: detail, loc, msg... there's nothing like that in the code examples. The API doesn't ask for these fields but it seems to be returning demanding these fields.
Please re-read the 1st paragraph of this post.
Thanks