Develop > Application Programming Interface > Legacy APIs > Artifact API > Add document to service offering

Add document to service offering

Details

URI /artifact/<service_offering_id>/document
Method POST
Parameters

userIdentifier=<user_id>
    Required; the user ID you want to use as credentials for this API call. See Get userIdentifier for the steps required to get the userIdentifier value.

Returns 200 - Ok
401 - Not authorized
404 - Object not found
500 - Server exception

Example

The following URL was sent with headers:

  • Content-type: multipart/form-data
  • Content-Disposition: form-data; name="file"
  • Content-Type: application/octet-stream

https://<host>:<port>/csa/rest/artifact/90cef5de3c63429f013c68b8cdda0bad/document?userIdentifier=90cef5de3c63429f013c642c7fc708ab

To better demonstrate how to use this REST API URI, see the sample python script that attaches a text file to a service offering. The script is created based on HTML documentation and you can find more information on HTML form at: http://www.w3.org/TR/html401/interact/forms.html#h-17.13.4.2.


""" Sample python script that attaches a text file in the current directory to 
an ACTIVE CSA service offering on a local host, with default credentials.
Script usage:
    <scrip-name.py> <service offering uuid> <filename> <admin uuid>
The following example attaches file doc.txt to the service offering with id 90cec0773c83a11a013c871e4c1a0503 using an admin user ID
90d96588360da0c701360da0f1d5f483
post-doc.py 90cec0773c83a11a013c871e4c1a0503 doc.txt 90d96588360da0c701360da0f1d5f483
Tested with: ActivePython 3.2.2.3
"""
from xml.dom.minidom import parse, parseString
from http.client import HTTPSConnection
from base64 import b64encode
import mimetypes
import sys
def get_content_type(filename):
    return mimetypes.guess_type(filename)[0] or 'application/octet-stream'
def get_file_contents(filename):
    CRLF = '\r\n'
    f = open(filename, 'r')
    return CRLF.join(f.readlines())
def encode_multipart_formdata(filename):
    BOUNDARY = '----------CSA_r0ck$_$'
    CRLF = '\r\n'
    L = []
    L.append('--' + BOUNDARY)
    L.append('Content-Disposition: form-data; name="file"; filename="%s"' % filename)
    L.append('Content-Type: %s' % get_content_type(filename))
    L.append('')
    L.append(get_file_contents(filename))
    L.append('--' + BOUNDARY + '--')
    L.append('')
    body = CRLF.join(L)
    content_type = 'multipart/form-data; boundary=%s' % BOUNDARY
    return content_type, body
def addDocumentToOffering(offeringId, documentName, userId):
    conn = HTTPSConnection("localhost:8444")
    userAndPass = b64encode(b"csaTransportUser:csaTransportUser").decode("ascii")
    post_url = "/csa/rest/artifact/" + offeringId + "/document?userIdentifier="+userId
    content_type, body = encode_multipart_formdata(documentName)
    content_length =  str(len(body))
    headers = {'Authorization' : 'Basic %s' %  userAndPass,
               'Content-Type' :'%s' % content_type, 'content-length' : str(len(body)), 'Accept' : 'application/xml'}
    conn.request('POST', post_url, body, headers=headers)
    res = conn.getresponse()
    data = res.read()
    print(data)
def main(offeringId, documentName, userId):
    addDocumentToOffering(offeringId, documentName, userId)
if __name__ == "__main__":
    offeringId = sys.argv[1]
    documentName = sys.argv[2]
    userId = sys.argv[3]
    main(offeringId, documentName, userId)