Sample code for 30+ languages & platforms
Visual FoxPro

Explaining the Rest ClearResponseBodyStream Method

See more Azure Cloud Storage Examples

The ClearResponseBodyStream method would be needed if your applicaiton called SetResponseBodyStream to send the response to a stream for one request, but then wants to subsequently do something else. The application must call ClearResponseBodyStream to no longer send responses to the stream.

Chilkat Visual FoxPro Downloads

Visual FoxPro
LOCAL lnSuccess
LOCAL loRest
LOCAL lnBTls
LOCAL lnPort
LOCAL lnBAutoReconnect
LOCAL loFileStream
LOCAL lnExpectedStatus
LOCAL lcResponseStr
LOCAL loBd

lnSuccess = 0

* This example requires the Chilkat API to have been previously unlocked.
* See Global Unlock Sample for sample code.

loRest = CreateObject('Chilkat.Rest')

* Connect to the web server
lnBTls = 1
lnPort = 443
lnBAutoReconnect = 1
lnSuccess = loRest.Connect("www.chilkatsoft.com",lnPort,lnBTls,lnBAutoReconnect)
IF (lnSuccess <> 1) THEN
    ? loRest.LastErrorText
    RELEASE loRest
    CANCEL
ENDIF

* Setup a file stream for the download
loFileStream = CreateObject('Chilkat.Stream')
loFileStream.SinkFile = "qa_output/starfish.jpg"

* Indicate that the call to FullRequestNoBody should send the response body
* to fileStream if the response status code is 200.
* If a non-success response status code is received, then nothing
* is streamed to the output file and the error response is returned by FullRequestNoBody.
lnExpectedStatus = 200
loRest.SetResponseBodyStream(lnExpectedStatus,1,loFileStream)

lcResponseStr = loRest.FullRequestNoBody("GET","/images/starfish.jpg")
IF (loRest.LastMethodSuccess = 0) THEN
    * Examine the request/response to see what happened.
    ? "response status code = " + STR(loRest.ResponseStatusCode)
    ? "response status text = " + loRest.ResponseStatusText
    ? "response header: " + loRest.ResponseHeader
    ? "response body (if any): " + lcResponseStr
    ? "---"
    ? "LastRequestStartLine: " + loRest.LastRequestStartLine
    ? "LastRequestHeader: " + loRest.LastRequestHeader
    RELEASE loRest
    RELEASE loFileStream
    CANCEL
ENDIF

? "downloaded starfish.jpg"

* Clear the response body stream previously set in the call to SetResponseBodyStream.
loRest.ClearResponseBodyStream()

* Download something else, but this time send the response body to bd.
loBd = CreateObject('Chilkat.BinData')
lnSuccess = loRest.FullRequestNoBodyBd("GET","/images/penguins.jpg",loBd)
IF (lnSuccess = 0) THEN
    * Examine the request/response to see what happened.
    ? "response status code = " + STR(loRest.ResponseStatusCode)
    ? "response status text = " + loRest.ResponseStatusText
    ? "response header: " + loRest.ResponseHeader
    ? "response body (if any): " + loBd.GetString("utf-8")
    ? "---"
    ? "LastRequestStartLine: " + loRest.LastRequestStartLine
    ? "LastRequestHeader: " + loRest.LastRequestHeader
    RELEASE loRest
    RELEASE loFileStream
    RELEASE loBd
    CANCEL
ENDIF

* Save the contents of bd to a file.
lnSuccess = loBd.WriteFile("qa_output/penguins.jpg")

? "Success: " + STR(lnSuccess)

RELEASE loRest
RELEASE loFileStream
RELEASE loBd