Sample code for 30+ languages & platforms
PureBasic

HTTP PUT JSON

See more HTTP Examples

Demonstrates how to send a JSON PUT and get the JSON response body.

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkHttpResponse.pb"
IncludeFile "CkHttp.pb"
IncludeFile "CkHttpRequest.pb"

Procedure ChilkatExample()

    success.i = 0

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

    req.i = CkHttpRequest::ckCreate()
    If req.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    http.i = CkHttp::ckCreate()
    If http.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    ; The PUT request to be sent will look like this:

    ; PUT /request HTTP/1.1
    ; Content-Type: application/jsonrequest
    ; Cookie: JSESSIONID=1234
    ; Content-Encoding: identity
    ; Host: json.penzance.org
    ; Accept: application/jsonrequest
    ; Accept-Encoding:
    ; Content-Length: 72
    ; 
    ; {"user":"doctoravatar@penzance.com","forecast":7,"t":"vlIj","zip":94089}

    ; First, remove default header fields that would be automatically
    ; sent.  (These headers are harmless, and shouldn't need to 
    ; be suppressed, but just in case...)
    CkHttp::setCkAcceptCharset(http, "")
    CkHttp::setCkUserAgent(http, "")
    CkHttp::setCkAcceptLanguage(http, "")

    ; Suppress the Accept-Encoding header by disallowing 
    ; a gzip response:
    CkHttp::setCkAllowGzip(http, 0)

    ; If a Cookie needs to be added...
    CkHttp::ckSetRequestHeader(http,"Cookie","JSESSIONID=1234")

    ; Add the Content-Encoding: identity header.
    CkHttp::ckSetRequestHeader(http,"Content-Encoding","identity")

    ; Modify the default "Accept" header:
    CkHttp::setCkAccept(http, "application/jsonrequest")

    jsonText.s = "{" + Chr(34) + "user" + Chr(34) + ":" + Chr(34) + "doctoravatar@penzance.com" + Chr(34) + "," + Chr(34) + "forecast" + Chr(34) + ":7," + Chr(34) + "t" + Chr(34) + ":" + Chr(34) + "vlIj" + Chr(34) + "," + Chr(34) + "zip" + Chr(34) + ":94089}"

    ; IMPORTANT: Make sure to change the URL, JSON text,
    ; and other data items to your own values.  The URL used
    ; in this example will not actually work.

    resp.i = CkHttpResponse::ckCreate()
    If resp.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkHttp::ckHttpStr(http,"PUT","http://json.penzance.org/request",jsonText,"utf-8","application/jsonrequest",resp)
    If success = 0
        Debug CkHttp::ckLastErrorText(http)
        CkHttpRequest::ckDispose(req)
        CkHttp::ckDispose(http)
        CkHttpResponse::ckDispose(resp)
        ProcedureReturn
    EndIf

    Debug "Response status code: " + Str(CkHttpResponse::ckStatusCode(resp))
    Debug "Response JSON:"
    Debug CkHttpResponse::ckBodyStr(resp)


    CkHttpRequest::ckDispose(req)
    CkHttp::ckDispose(http)
    CkHttpResponse::ckDispose(resp)


    ProcedureReturn
EndProcedure