Example: upload a file as attachment in request body
Shows a sample function uploading an image file as attachment in the message request body.
In order to send a file as an attachment to the Web service, set the WSAttachment attribute on an input parameter.
Example: upload file as attachment using WSAttachment
- An input paramater "fname" is defined as type
STRING
with aWSAttachment
attribute. The REST engine treats the parameter value as a path to a file to be attached. - The
WSMedia
attribute handles the data format for images. The wildcard (image/*
) in WSMedia allows for all image types. If the file can be any type,WSMedia
is not specified withWSAttachment
.
The GWS stores the file in a temporary directory, and returns the absolute path to the file in the input parameter ("fname"). When the call to the function ends, the file will be removed unless it is saved to a location on disk.
The new_fname variable is set to create a path to the current directory, to which the filename is added with the os.Path.baseName instruction. The os.Path.rename instruction moves the file from the temporary directory to the current directory.
IMPORT os
PUBLIC FUNCTION UploadFile( fname STRING ATTRIBUTES(WSAttachment,
WSMedia = "image/*") )
ATTRIBUTES (WSPost,
WSPath = "/files2/fetch")
RETURNS STRING ATTRIBUTES(WSMedia = "text/plain")
DEFINE ret, new_fname STRING
DEFINE ok INTEGER
LET new_fname = os.Path.pwd()||os.Path.separator()||os.Path.baseName(fname)
LET ok = os.Path.rename(fname,new_fname)
LET ret = SFMT("Got image: %1",new_fname)
RETURN ret
END FUNCTION
In Figure 1 the image is sent as an attachment in the message body.