Sample code for 30+ languages & platforms
Tcl

Example: Http.HttpReq method

Demonstrates how to call the HttpReq method.

Chilkat Tcl Downloads

Tcl

load ./chilkat.dll

set success 0

set http [new_CkHttp]

set success 0

# This example will send an HTTP POST with the body of the HTTP request containing JSON.

# Create the following JSON: { "name": "Harry", "state": "FL" }

set json [new_CkJsonObject]

CkJsonObject_UpdateString $json "name" "Harry"
CkJsonObject_UpdateString $json "state" "FL"

# Specify the details of the request
set req [new_CkHttpRequest]

CkHttpRequest_put_HttpVerb $req "POST"
CkHttpRequest_put_ContentType $req "application/json"
CkHttpRequest_LoadBodyFromString $req [CkJsonObject_emit $json] "utf-8"

# Send the charset attribute in the HTTP request header.
CkHttpRequest_put_SendCharset $req 1
CkHttpRequest_put_Charset $req "utf-8"

# Send the POST
# You can send the POST to the URL below.  
# The response will contain the raw body of the request that was sent.
# (i.e. everything except the HTTP request header).

set resp [new_CkHttpResponse]

set success [CkHttp_HttpReq $http "https://www.chilkatsoft.com/echo_request_body.asp" $req $resp]
if {$success == 0} then {
    puts [CkHttp_lastErrorText $http]
    delete_CkHttp $http
    delete_CkJsonObject $json
    delete_CkHttpRequest $req
    delete_CkHttpResponse $resp
    exit
}

# Examine the HTTP request header we sent:
puts [CkHttp_lastHeader $http]

# The response body contains the raw content of the HTTP request body we sent.
puts [CkHttpResponse_bodyStr $resp]

# Sample output:

# POST /echo_request_body.asp HTTP/1.1
# Host: www.chilkatsoft.com
# Content-Type: application/json; charset=utf-8
# Content-Length: 29
# 
# 
# {"name":"Harry","state":"FL"}

delete_CkHttp $http
delete_CkJsonObject $json
delete_CkHttpRequest $req
delete_CkHttpResponse $resp