PureBasic
PureBasic
TicketBAI -- Send HTTP POST
See more TicketBAI Examples
Demonstrates how to send a TicketBAI POST and get the response.Chilkat PureBasic Downloads
IncludeFile "CkHttpResponse.pb"
IncludeFile "CkHttp.pb"
IncludeFile "CkStringBuilder.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.
http.i = CkHttp::ckCreate()
If http.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
success = CkHttp::ckSetSslClientCertPfx(http,"your.pfx","pfx_password")
If success = 0
Debug CkHttp::ckLastErrorText(http)
CkHttp::ckDispose(http)
ProcedureReturn
EndIf
; Get the XML we wish to send in the body of the request.
sbXml.i = CkStringBuilder::ckCreate()
If sbXml.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
success = CkStringBuilder::ckLoadFile(sbXml,"qa_data/payload.xml","utf-8")
If success = 0
Debug "Failed to load XML that is to be the HTTP request body"
CkHttp::ckDispose(http)
CkStringBuilder::ckDispose(sbXml)
ProcedureReturn
EndIf
; Build the following JSON
; {
; "con": "LROE",
; "apa": "1.1",
; "inte": {
; "nif": "número de identificación fiscal",
; "nrs": "nombre o Razón social",
; "ap1": "primer apellido",
; "ap2": "segundo apellido"
; },
; "drs": {
; "mode": "140",
; "ejer": "ejercicio"
; }
; }
; Use this online tool to generate code from sample JSON:
; Generate Code to Create JSON
json.i = CkJsonObject::ckCreate()
If json.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkJsonObject::ckUpdateString(json,"con","LROE")
CkJsonObject::ckUpdateString(json,"apa","1.1")
CkJsonObject::ckUpdateString(json,"inte.nif","número de identificación fiscal")
CkJsonObject::ckUpdateString(json,"inte.nrs","nombre o Razón social")
CkJsonObject::ckUpdateString(json,"inte.ap1","primer apellido")
CkJsonObject::ckUpdateString(json,"inte.ap2","segundo apellido")
CkJsonObject::ckUpdateString(json,"drs.mode","140")
CkJsonObject::ckUpdateString(json,"drs.ejer","ejercicio")
; Add required headers...
CkHttp::ckSetRequestHeader(http,"eus-bizkaia-n3-version","1.0")
CkHttp::ckSetRequestHeader(http,"eus-bizkaia-n3-content-type","application/xml")
CkHttp::ckSetRequestHeader(http,"eus-bizkaia-n3-data",CkJsonObject::ckEmit(json))
url.s = "https://pruesarrerak.bizkaia.eus/N3B4000M/aurkezpena"
resp.i = CkHttpResponse::ckCreate()
If resp.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkHttp::setCkUncommonOptions(http, "SendGzipped")
success = CkHttp::ckHttpSb(http,"POST",url,sbXml,"utf-8","application/octet-stream",resp)
If success = 0
Debug CkHttp::ckLastErrorText(http)
CkHttp::ckDispose(http)
CkStringBuilder::ckDispose(sbXml)
CkJsonObject::ckDispose(json)
CkHttpResponse::ckDispose(resp)
ProcedureReturn
EndIf
CkHttp::ckClearHeaders(http)
Debug "response status code: " + Str(CkHttpResponse::ckStatusCode(resp))
; Examine the response (it is already decompressed)
Debug "response body:"
Debug CkHttpResponse::ckBodyStr(resp)
CkHttp::ckDispose(http)
CkStringBuilder::ckDispose(sbXml)
CkJsonObject::ckDispose(json)
CkHttpResponse::ckDispose(resp)
ProcedureReturn
EndProcedure