Example: upload a file 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( fn STRING ATTRIBUTES(WSAttachment, WSMedia="image/*") )
  ATTRIBUTES (WSPost,
              WSPath="/files2/fetch")
  RETURNS STRING ATTRIBUTE(WSMedia="text/html")
    DEFINE ret, new_fn STRING
    DEFINE ok INTEGER
    DISPLAY "Got file at ",fn
    LET new_fn = os.Path.baseName(fn)
    LET ok = os.Path.delete(new_fn)
    LET ok = os.Path.rename(fn,new_fn)
    LET ret = SFMT("Got image: %1",new_fn)
    RETURN ret
END FUNCTION

In order to send a file as an attachment, you must have one input parameter only. In the sample function, "fn" is defined as type STRING with a WSAttachment, and WSMedia attribute to handle the data format for images.

Figure: Output of HTTP request


The image is sent as an attachment in the message body.