Page 1 of 1

Send file with POST request

PostPosted: Sun Sep 24, 2023 10:46 am
by 258.d.852
I have a servoy-app and an external service, so-called, store-file-service.

In store-file-service (written in Spring), I have a controller that takes responsibility of storing file via a POST request with request param is "file-param" and the paramter is MultipartFile.

In servoy-app, to send a POST request with the parameterName is "file-param", I have the following code

Code: Select all
   var client = plugins.http.createNewHttpClient();
   var poster = client.createPostRequest("http://localhost:8080/api/store-file");
   
   // configure header
   poster.addHeader("Content-Type", "multipart/form-data; boundary=any")
   // ===

   // configure parameter
   poster.addFile("file-param", "filename", myJsFile);
   // ===
   
   var response = poster.executeRequest();   
   response.close();


From servoy-app, everything seems to work. But from store-file-service, I got log info

MissingServletRequestPartException: Required part 'file-param' is not present.


This makes me confused because I already declared the parameterName while addFile as "file-param". Is there any wrong with my configuration in servoy-app?

Testing store-file-service with postman works well as expected.

Re: Send file with POST request

PostPosted: Mon Sep 25, 2023 10:02 am
by mboegem
Hi,

I assume you are using a recent version of Servoy.
For me this code works:
Code: Select all
var client = plugins.http.createNewHttpClient();
   var poster = client.createPostRequest("http://localhost:8080/api/store-file");
   
   // configure header
   poster.addHeader('Accept', 'application/json')
   // ===

   // configure parameter
   poster.addFile("file-param", "filename", myJsFile, 'application/octet-stream');
   poster.forceMultipart(true);
   // ===
   
   var response = poster.executeRequest();   
   response.close();


The above is processed by a web service written in Servoy and using the rest-ws plugin.

An easy way to test incoming requests is this service: https://requestbin.com

Hope this helps

Re: Send file with POST request

PostPosted: Mon Sep 25, 2023 8:52 pm
by 258.d.852
Dear Marc,

Wonderful, It works like a charm!

Thanks for your quick feedback :-)