artyom-beilis/cppcms

Failed to upload files from REST API I create with cppcms

Hethsron opened this issue · 19 comments

HI

I'm using CPPCMS to create a RESTful API. But now i have a little problem and i need your help. I want to allow external client to upload files like image using curl. But i wonder myself how to get this file and save it on the server.
Example of command i want the client use:
curl -i --data @image.png --url http://myweberser.com:8080/api/upload

In my header I have done this:
...
dispatcher().map("POST", "/upload", &myapi::uploaddata, this);

mapper().root("/api");
...

In my source i have done this 👍
void myapi::uploaddata() {
try {
if (request().request_method() == "POST") {
// What can i do here
}
}
catch (std::exception const &e) {
std::cerr << "Failed to push the data" << std::endl;
std::cerr << e.what() << std::endl;
}
}

I don't want to create a web page with a form. I juste want to upload data without using traditional method to upload files from a website

Hi, you can with:
std::pair<void *,size_t> raw = request().raw_post_data();

Remember to add the appropriate content-type.

thanks

@diegodfrf is there a possibility to limit the size of the file?

Three options:

  1. Before sending it to the server.
  2. When the file is uploaded, you decide if you want to save the file.
  3. config.js set:
    "security" : {
    "content_length_limit": 10240 (Size in KB)
    }

CppCMS 1.2 Input Content Filtering
request().limits().multipart_form_data_limit(len)

@diegodfrf Thank you! Thank you! It works. It works.

@diegodfrf If i want to use multimap keys to get posted data, how can I do this?

I want to implement a request that will allow me to retrieve strings and binary data

And I want to use : std::multimapstd::string,std::string data = request().post();

And I wonder how to do that.

@diegodfrf Could you help me please?

@artyom-beilis Hi. I would like to implement a request that allows me to send both binary data such as an image and a character string. How can I do that?

if I want this request to work : curl -X POST -F 'image=@/path/to/pictures/picture.jpg' --data "param1=value1&param2=value2 http://domain.tld/upload

CppCMS 1.2 Input Content Filtering
request().limits().multipart_form_data_limit(len)

Thanks @masaoliou . I didn't know. If I upload a 3GB file without a form, it shows error ERR_CONNECTION_RESET after 10 seconds, with security.content_length_limit configured, but if I upload it with a form, this does not happen. What could it be?

@diegodfrf I need to sent image as request to server and the server needs to perform edge detection in the image and need to send the response as edge detected image using CppCMS do you have any idea or the code to implement it,could you please help me