artyom-beilis / cppcms

CppCMS Framework
Other
443 stars 107 forks source link

Failed to upload files from REST API I create with cppcms #54

Open Hethsron opened 5 years ago

Hethsron commented 5 years ago

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

Hethsron commented 5 years ago

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

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

Hethsron commented 5 years ago

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; } }

Hethsron commented 5 years ago

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

diegodfrf commented 5 years ago

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

Remember to add the appropriate content-type.

Hethsron commented 5 years ago

thanks

Hethsron commented 5 years ago

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

diegodfrf commented 5 years ago

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) }
masaoliou commented 5 years ago

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

Hethsron commented 5 years ago

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

Hethsron commented 5 years ago

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

Hethsron commented 5 years ago

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

Hethsron commented 5 years ago

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

Hethsron commented 5 years ago

And I wonder how to do that.

Hethsron commented 5 years ago

@diegodfrf Could you help me please?

Hethsron commented 5 years ago

@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?

Hethsron commented 5 years ago

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

diegodfrf commented 5 years ago

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 commented 5 years ago

@Hethsron with form-data https://stackoverflow.com/questions/4238809/example-of-multipart-form-data

balasubramanian99 commented 3 years ago

@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