| HTTP classes / The HTTPPart class | |
Examples using methods of the com.HTTPPart class.
This example consists of two applications: a client and server exchanging an XML document in multipart with a image as an attachment.
IMPORT com
IMPORT xml
CONSTANT SERVER_URL = "http://localhost:8090/MultipartMixed/Sample"
MAIN
DEFINE req com.HTTPRequest
DEFINE resp com.HTTPResponse
DEFINE doc xml.DomDocument
DEFINE root xml.DomNode
DEFINE p com.HTTPPart
DEFINE type STRING
DEFINE count INTEGER
DEFINE ind INTEGER
LET req = com.HTTPRequest.Create(SERVER_URL)
CALL req.setMethod("POST")
CALL req.setHeader("MyClientHeader","Hello")
TRY
# Set multipart type
CALL req.setMultipartType("mixed",NULL,NULL)
# Add filename as part
LET p = com.HTTPPart.CreateAttachment("my_picture.png")
# Set attachment Content-Type
CALL p.setHeader("Content-Type","image/png")
# Add part to the request
CALL req.addPart(p)
# Perform XML request
LET doc = xml.DomDocument.CreateDocument("MyXmlDocument")
CALL req.doXmlRequest(doc)
# Check response
LET resp=req.getResponse()
IF resp.getStatusCode() != 200 THEN
DISPLAY "HTTP Error ("||resp.getStatusCode()||") ",
resp.getStatusDescription()
EXIT PROGRAM (-1)
END IF
IF resp.getStatusDescription() != "OK" THEN
DISPLAY "HTTP Error ("||resp.getStatusCode()||") ",
resp.getStatusDescription()
EXIT PROGRAM (-1)
END IF
# Check whether multipart response or not
LET type = resp.getMultipartType()
IF type IS NULL THEN
DISPLAY "Failed : Expected multipart in response"
EXIT PROGRAM (-1)
ELSE
DISPLAY "Response is multipart of :",type
END IF
# Check response
LET doc = resp.getXmlResponse()
IF doc IS NULL THEN
DISPLAY "Expected XML document as response"
EXIT PROGRAM (-1)
ELSE
DISPLAY "Response is : ",doc.saveToString()
END IF
# Process additional parts
FOR ind = 1 TO resp.getPartCount()
LET p = resp.getPart(ind)
IF p.getAttachment() IS NOT NULL THEN
DISPLAY "Attached file at :",p.getAttachment()
ELSE
DISPLAY "Attached part is :",p.getContentAsString()
END IF
END FOR
CATCH
DISPLAY "unexpected exception :",STATUS," ("||SQLCA.SQLERRM||")"
EXIT PROGRAM (-1)
END TRY
END MAIN
IMPORT com
IMPORT xml
MAIN
DEFINE req com.HTTPServiceRequest
DEFINE url STRING
DEFINE method STRING
DEFINE txt STRING
DEFINE doc xml.DomDocument
DEFINE type STRING
DEFINE ind INTEGER
DEFINE p com.HTTPPart
CALL com.WebServiceEngine.Start()
LET req = com.WebServiceEngine.getHTTPServiceRequest(-1)
LET url = req.getURL()
IF url IS NULL THEN
DISPLAY "Failed: url should not be null"
EXIT PROGRAM (-1)
END IF
LET method = req.getMethod()
IF method IS NULL OR method != "POST" THEN
DISPLAY "Failed: method should be POST"
EXIT PROGRAM (-1)
END IF
# Check multipart type
LET type = req.getRequestMultipartType()
IF type IS NULL THEN
DISPLAY "Failed: expected multipart in request"
EXIT PROGRAM (-1)
END IF
TRY
LET doc = req.readXMLRequest()
DISPLAY "Request is :", doc.saveToString()
CATCH
DISPLAY "Failed: unexpected error :", STATUS
EXIT PROGRAM (-1)
END TRY
# Process additional parts
FOR ind = 1 TO req.getRequestPartCount()
LET p = req.getRequestPart(ind)
IF p.getAttachment() IS NOT NULL THEN
DISPLAY "Attached file at :",p.getAttachment()
ELSE
DISPLAY "Attached part is :",p.getContentAsString()
END IF
END FOR
# Set multipart response type
CALL req.setResponseMultipartType("mixed",NULL,NULL)
# Add XML Part
LET p = com.HTTPPart.CreateAttachment("my_other_picture.jpg")
CALL p.setHeader("Content-Type","image/jpg")
CALL req.addResponsePart(p)
LET doc = xml.DomDocument.CreateDocument("MyResponse")
CALL req.sendXmlResponse(200,NULL,doc)
END MAIN