PureBasic
PureBasic
HTTP GET with Non-USASCII Query Params
See more HTTP Examples
This example illustrates how query parameters in a URL are typically encoded and transmitted.Chilkat PureBasic Downloads
IncludeFile "CkStringBuilder.pb"
IncludeFile "CkHttp.pb"
Procedure ChilkatExample()
success.i = 0
; This example assumes the Chilkat HTTP API to have been previously unlocked.
; See Global Unlock Sample for sample code.
; First, let's load the string "Grünhöfer GmbH" from a file. (This is a fictitious company name.)
; The file uses the utf-8 charset encoding.
sbCompanyName.i = CkStringBuilder::ckCreate()
If sbCompanyName.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
success = CkStringBuilder::ckLoadFile(sbCompanyName,"qa_data/txt/companyName.txt","utf-8")
; Assuming success for this example...
; We'll send an HTTP GET request to https://chilkatsoft.com/example?company_name={company name}
; When sending an HTTP GET request with query parameters that contain accented characters
; (e.g., umlauts: ä, ö, ü), they must be percent-encoded (URL encoded) to ensure proper transmission and
; interpretation by the server. This is based on their UTF-8 byte values.
sbUrl.i = CkStringBuilder::ckCreate()
If sbUrl.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkStringBuilder::ckAppend(sbUrl,"https://chilkatsoft.com/example?company_name=")
CkStringBuilder::ckAppend(sbUrl,CkStringBuilder::ckGetEncoded(sbCompanyName,"url","utf-8"))
http.i = CkHttp::ckCreate()
If http.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
; Send the following HTTP GET request:
; GET /example?company_name=Gr%C3%BCnh%C3%B6fer%20GmbH HTTP/1.1
; Host: chilkatsoft.com
; Accept: */*
; Accept-Encoding: gzip
sbResponse.i = CkStringBuilder::ckCreate()
If sbResponse.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
success = CkHttp::ckQuickGetSb(http,CkStringBuilder::ckGetAsString(sbUrl),sbResponse)
If success = 0
Debug CkHttp::ckLastErrorText(http)
CkStringBuilder::ckDispose(sbCompanyName)
CkStringBuilder::ckDispose(sbUrl)
CkHttp::ckDispose(http)
CkStringBuilder::ckDispose(sbResponse)
ProcedureReturn
EndIf
Debug "response status code: " + Str(CkHttp::ckLastStatus(http))
Debug CkStringBuilder::ckGetAsString(sbResponse)
CkStringBuilder::ckDispose(sbCompanyName)
CkStringBuilder::ckDispose(sbUrl)
CkHttp::ckDispose(http)
CkStringBuilder::ckDispose(sbResponse)
ProcedureReturn
EndProcedure