PureBasic
PureBasic
Debug REST HTTP Request
See more REST Examples
Demonstrates how to generate the HTTP Request (with all headers intact) without actually sending the request.Note: This example requires Chilkat v9.5.0.77 or later.
Chilkat PureBasic Downloads
IncludeFile "CkBinData.pb"
IncludeFile "CkStringBuilder.pb"
IncludeFile "CkRest.pb"
IncludeFile "CkJsonObject.pb"
Procedure ChilkatExample()
success.i = 0
; This example requires the Chilkat API to have been previously unlocked.
; See Global Unlock Sample for sample code.
; This example will connect to the web server, but does not actually send a request.
; When in DebugMode, the request is composed in memory and can be retrieved by calling
; GetLastDebugRequest.
rest.i = CkRest::ckCreate()
If rest.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
; Connect Code...
; URL: https://test-api.service.hmrc.gov.uk/organisations/vat/MY_HMRC_VRN/returns
bTls.i = 1
port.i = 443
bAutoReconnect.i = 1
success = CkRest::ckConnect(rest,"test-api.service.hmrc.gov.uk",port,bTls,bAutoReconnect)
If success <> 1
Debug "ConnectFailReason: " + Str(CkRest::ckConnectFailReason(rest))
Debug CkRest::ckLastErrorText(rest)
CkRest::ckDispose(rest)
ProcedureReturn
EndIf
; Build the request body...
json.i = CkJsonObject::ckCreate()
If json.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkJsonObject::ckUpdateString(json,"periodKey","A001")
CkJsonObject::ckUpdateNumber(json,"vatDueSales","105.50")
CkJsonObject::ckUpdateNumber(json,"vatDueAcquisitions","-100.45")
CkJsonObject::ckUpdateNumber(json,"totalVatDue","5.05")
CkJsonObject::ckUpdateNumber(json,"vatReclaimedCurrPeriod","105.15")
CkJsonObject::ckUpdateNumber(json,"netVatDue","100.10")
CkJsonObject::ckUpdateInt(json,"totalValueSalesExVAT",300)
CkJsonObject::ckUpdateInt(json,"totalValuePurchasesExVAT",300)
CkJsonObject::ckUpdateInt(json,"totalValueGoodsSuppliedExVAT",3000)
CkJsonObject::ckUpdateInt(json,"totalAcquisitionsExVAT",3000)
CkJsonObject::ckUpdateBool(json,"finalised",1)
; Add Headers...
CkRest::ckAddHeader(rest,"Accept","application/vnd.hmrc.1.0+json")
CkRest::ckAddHeader(rest,"Authorization","Bearer HMRC_ACCESS_TOKEN")
CkRest::ckAddHeader(rest,"Content-Type","application/json")
sbRequestBody.i = CkStringBuilder::ckCreate()
If sbRequestBody.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkJsonObject::ckEmitSb(json,sbRequestBody)
; Set DebugMode so that no request is actually sent.
CkRest::setCkDebugMode(rest, 1)
sbResponseBody.i = CkStringBuilder::ckCreate()
If sbResponseBody.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
success = CkRest::ckFullRequestSb(rest,"POST","/organisations/vat/MY_HMRC_VRN/returns",sbRequestBody,sbResponseBody)
If success <> 1
Debug CkRest::ckLastErrorText(rest)
CkRest::ckDispose(rest)
CkJsonObject::ckDispose(json)
CkStringBuilder::ckDispose(sbRequestBody)
CkStringBuilder::ckDispose(sbResponseBody)
ProcedureReturn
EndIf
; Get the exact contents of what would've been sent.
; This includes the HTTP start line, the HTTP request headers, and the request body.
; Given that it's possible for the request body to contain binary data,
; the GetLastDebugRequest fetches into a BinData object.
; In this case, however, our request body contained JSON, so we can
; examine it as a string..
bdRequest.i = CkBinData::ckCreate()
If bdRequest.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
success = CkRest::ckGetLastDebugRequest(rest,bdRequest)
Debug "----"
Debug CkBinData::ckGetString(bdRequest,"utf-8")
Debug "----"
; The output for the above case:
; POST /organisations/vat/MY_HMRC_VRN/returns HTTP/1.1
; Accept: application/vnd.hmrc.1.0+json
; Host: test-api.service.hmrc.gov.uk
; Authorization: Bearer HMRC_ACCESS_TOKEN
; Content-Type: application/json
; Content-Length: 281
;
; {"periodKey":"A001","vatDueSales":105.50, ... ,"finalised":true}
;
;
CkRest::ckDispose(rest)
CkJsonObject::ckDispose(json)
CkStringBuilder::ckDispose(sbRequestBody)
CkStringBuilder::ckDispose(sbResponseBody)
CkBinData::ckDispose(bdRequest)
ProcedureReturn
EndProcedure