Example: upload a file as attachment in request body
Shows a sample function sending an image file as attachment in the message request body.
Example 1: upload file as attachment using WSAttachment
IMPORT os
PUBLIC FUNCTION UploadFile( fname STRING ATTRIBUTES(WSAttachment, WSMedia="image/*") )
ATTRIBUTES (WSPost,
WSPath="/files2/fetch")
RETURNS STRING ATTRIBUTE(WSMedia="text/plain")
DEFINE ret, new_fname STRING
DEFINE ok INTEGER
LET new_fname = os.Path.pwd()||os.Path.separator()||new_fname
LET ok = os.Path.rename(fname,new_fname)
LET ret = SFMT("Got image: %1",new_fname)
RETURN ret
END FUNCTION
In order to send a file as an attachment to the service, set the WSAttachment attribute on an input parameter. In
the sample function, "fname" is defined as type STRING
with a
WSAttachment
. The WSMedia
attribute handles the data
format for images.
The GWS stores the file in a temporary directory, and returns the absolute path to the file in the input parameter (fname). At the end of the REST function, the file will be removed unless you save it to a location on disk. You can use the os.Path.rename instruction to move the file from the temporary directory to a directory of your choice.
The image is sent as an attachment in the message body.