Send file with POST request

Questions and answers on designing your Servoy solutions, database modelling and other 'how do I do this' that don't fit in any of the other categories

Send file with POST request

Postby 258.d.852 » Sun Sep 24, 2023 10:46 am

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.
258.d.852
 
Posts: 10
Joined: Wed Jul 12, 2023 1:10 pm

Re: Send file with POST request

Postby mboegem » Mon Sep 25, 2023 10:02 am

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
Marc Boegem
Solutiative / JBS Group, Partner
Servoy Specialist
• Servoy Certified Developer
• Servoy Valued Professional
• Freelance Developer

Image
User avatar
mboegem
 
Posts: 1752
Joined: Sun Oct 14, 2007 1:34 pm
Location: Amsterdam

Re: Send file with POST request

Postby 258.d.852 » Mon Sep 25, 2023 8:52 pm

Dear Marc,

Wonderful, It works like a charm!

Thanks for your quick feedback :-)
258.d.852
 
Posts: 10
Joined: Wed Jul 12, 2023 1:10 pm


Return to Programming with Servoy

Who is online

Users browsing this forum: No registered users and 26 guests