PureBasic
PureBasic
QR Code Generator via api.qrserver.com REST API
See more HTTP Examples
Demonstrates how to generate a QR code using the api.qrserver.com REST API service.Chilkat PureBasic Downloads
IncludeFile "CkHttpResponse.pb"
IncludeFile "CkHttp.pb"
IncludeFile "CkBinData.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
; Send the following GET request to get a binary response.
; The body of the binary response contains the image data for the QR code.
; https://api.qrserver.com/v1/create-qr-code/?data=[URL-encoded-text]&size=[pixels]x[pixels]
queryParams.i = CkJsonObject::ckCreate()
If queryParams.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkJsonObject::ckUpdateString(queryParams,"data","Hello World")
CkJsonObject::ckUpdateString(queryParams,"size","100x100")
; Get a PNG file..
; Possible formats are:
; png
; gif
; jpeg
; jpg
; svg
; eps
; Case matters. Use lowercase.
CkJsonObject::ckUpdateString(queryParams,"format","png")
; Send the GET request to an endpoint.
; Chilkat will add the url-encoded query params passed in the JSON.
resp.i = CkHttpResponse::ckCreate()
If resp.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
success = CkHttp::ckHttpParams(http,"GET","https://api.qrserver.com/v1/create-qr-code/",queryParams,resp)
If success = 0
Debug CkHttp::ckLastErrorText(http)
CkHttp::ckDispose(http)
CkJsonObject::ckDispose(queryParams)
CkHttpResponse::ckDispose(resp)
ProcedureReturn
EndIf
; Did it succeed?
If CkHttpResponse::ckStatusCode(resp) <> 200
; The response body, if anything, would not be the image data, but would likely be the error text (or HTML, or whatever...)
Debug CkHttpResponse::ckBodyStr(resp)
Debug "Response status = " + Str(CkHttpResponse::ckStatusCode(resp))
Debug "Failed."
CkHttp::ckDispose(http)
CkJsonObject::ckDispose(queryParams)
CkHttpResponse::ckDispose(resp)
ProcedureReturn
EndIf
; Success if we get here..
; Save the binary body as the PNG file, or you can get the bytes of the PNG..
success = CkHttpResponse::ckSaveBodyBinary(resp,"c:/temp/qa_output/qr_code.png")
; Or get the bytes:
bd.i = CkBinData::ckCreate()
If bd.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkHttpResponse::ckGetBodyBd(resp,bd)
; Use the bytes in bd...
; See the online reference documentation for the function to access the bytes directly.
Debug "Success."
CkHttp::ckDispose(http)
CkJsonObject::ckDispose(queryParams)
CkHttpResponse::ckDispose(resp)
CkBinData::ckDispose(bd)
ProcedureReturn
EndProcedure