How send REST POST request including strings + photo

83 views Asked by At

I am using cpprestsdk to create a desktop client for an external API. I'm stuck on a POST request for registration, in which I need to send strings and a photo.

I plan to make the registry through such a function: bool POSTUser(string_t &token, string_t &name, string_t &email, string_t &phone, string_t &photoPath);

I suppose I will need to use this overload: web::http::client::http_client::request, because the responses from the server also come in json, and this filestream but I have no idea how to cram it json, and what else is _CharType in the template???

Example in the server API documentation:

  • name - string
  • email - string
  • phone - string
  • position_id - string
  • photo - user photo should be jpg/jpeg image

Example of method url: https://server.server/api/v1/users

Example curl sh:

$curl --form "photo=@example/photo.jpg;" \ --form name=Jhon \ --form [email protected] \ --form phone=+380955388485 \ --form position_id=2 \ https://server.server/api/v1/users

Example javascript:

var formData = new FormData(); // file from input type='file'
var fileField = document.querySelector('input[type="file"]');
formData.append('position_id', 2);
formData.append('name', 'Jhon');
formData.append('email', '[email protected]');
formData.append('phone', '+380955388485');
formData.append('photo', fileField.files[0]);
fetch('https://server.server/api/v1/users', {
  method: 'POST', body: formData, headers: {
    'Token': token, // get token with GET api/v1/token method 
  },
}).then(function(response) { return response.json(); })
.then(function(data) {
  console.log(data);
  if(data.success) { 
    // process success response 
  } else {
    // proccess server errors
  } 
}).catch(function(error) { 
  // proccess network errors
});
0

There are 0 answers