Jump to content
Sign in to follow this  
Yaron

Handling MultipartFormData

Recommended Posts

I am trying to upload JSON data and an image as a MultipartFormData and return a JSON string from mars using the following code:
 

var

  shareData  : TMultipartFormData;

  jsonStr : String;

begin

  shareData := TMultipartFormData.Create;
  shareData.AddField('json','{"empty":"empty"}');
  shareData.AddFile('image',FileName);

  jsonStr  := NetHTTPRequest.Post(urlAPIBase+urlAPIShareImage,shareData).ContentAsString(TEncoding.UTF8);

end;

 

How do I setup mars to give me access to the json field and image fields?

 

I looked at the ContentTypes demo, but I wasn't sure if any of the examples actually covered MultipartFormData.

Share this post


Link to post

Hi, there was no demo showcasing how to handle multipart/form-data on the server side with MARS. But, since a couple of minutes, there is! 🙂

Have a look here: https://github.com/andrea-magni/MARS/tree/master/Demos/MultipartFormData

 

Build and run the demo, then use a browser to navigate http://localhost:8080/rest/default/helloworld/1 to have a simple HTML page with a multipart/form-data form.

The last part of the URL can be 1, 2, 3 or 4.

On the server side, you will notice four methods marked with the POST attribute, to handle the 4 requests. It is actually the same http request served through 4 different available ways in MARS (see here: https://github.com/andrea-magni/MARS/blob/master/Demos/MultipartFormData/Server.Resources.pas😞

 

1) ask the library to inject a dynamic array of TFormParam:

[FormParams] AParams: TArray<TFormParam>

Implementation of the REST method loops on that array to provide a result value;

 

2) target specific params (by name) using the following syntax:
 

[FormParam('json')] AJSON: TFormParam; 
[FormParam('image')] AImage: TFormParam;

FormParam is defined in MARS.Core.Utils and can represent both simple parameters and files;

 

3) push a bit more and ask for automatic deserialization of the first parameter into a TJSONObject. The methods has these arguments defined: 

[FormParam('json'), Consumes(TMediaType.Application_JSON)] AJSON: TJSONObject; 
[FormParam('image')] AImage: TFormParam;

Note the type of the first argument is no more TFormParam but TJSONObject

 

4) last version takes advantage of JSON to record serialization (built-in with MARS):

[FormParam('json'), Consumes(TMediaType.Application_JSON)] ARecord: TPerson;
[FormParam('image')] AImage: TFormParam;

 

All these four variations have a TMyResult record as return value. This record gets automatically serialized to JSON by the library.

You should be able to build clients in any language (I tested with Postman and the HTML page, using Chrome).

 

Let me know if everything is clear and working.

 

Sincerely,

Andrea

  • Like 1

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  
×