cppcms icon indicating copy to clipboard operation
cppcms copied to clipboard

Failed to upload files from REST API I create with cppcms

Open Hethsron opened this issue 6 years ago • 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

Hethsron avatar Feb 23 '19 17:02 Hethsron

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

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

Hethsron avatar Feb 23 '19 17:02 Hethsron

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 avatar Feb 23 '19 17:02 Hethsron

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

Hethsron avatar Feb 23 '19 17:02 Hethsron

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

Remember to add the appropriate content-type.

diegodfrf avatar Apr 10 '19 02:04 diegodfrf

thanks

Hethsron avatar Apr 10 '19 04:04 Hethsron

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

Hethsron avatar Apr 14 '19 13:04 Hethsron

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

diegodfrf avatar Apr 14 '19 23:04 diegodfrf

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

masaoliou avatar Apr 15 '19 06:04 masaoliou

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

Hethsron avatar Apr 15 '19 13:04 Hethsron

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

Hethsron avatar Apr 15 '19 15:04 Hethsron

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

Hethsron avatar Apr 15 '19 15:04 Hethsron

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

Hethsron avatar Apr 15 '19 15:04 Hethsron

And I wonder how to do that.

Hethsron avatar Apr 15 '19 15:04 Hethsron

@diegodfrf Could you help me please?

Hethsron avatar Apr 15 '19 15:04 Hethsron

@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 avatar Apr 16 '19 12:04 Hethsron

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

Hethsron avatar Apr 16 '19 12:04 Hethsron

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 avatar Apr 19 '19 03:04 diegodfrf

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

diegodfrf avatar Apr 19 '19 04:04 diegodfrf

@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

balasubramanian99 avatar Mar 19 '21 07:03 balasubramanian99