Sample code for 30+ languages & platforms
PowerBuilder Requires Chilkat v11.0.0+

Example: Http.HttpJson method - Send a POST with JSON Body

See more HTTP Examples

Demonstrates how to call the HttpJson method, which sends an HTTP POST with a JSON body. This example sends the following request:
POST /echo_request_body.asp HTTP/1.1
Host: chilkatsoft.com
Accept: */*
Accept-Encoding: gzip
Content-Type: application/json
Content-Length: 26

{"greeting":"Hello World"}

Also see: Chilkat Http Default and Auto-Filled Headers

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_Http
oleobject loo_Json
string ls_Url
oleobject loo_Resp

li_Success = 0

loo_Http = create oleobject
li_rc = loo_Http.ConnectToNewObject("Chilkat.Http")
if li_rc < 0 then
    destroy loo_Http
    MessageBox("Error","Connecting to COM object failed")
    return
end if
li_Success = 0

loo_Json = create oleobject
li_rc = loo_Json.ConnectToNewObject("Chilkat.JsonObject")

//  Build the following JSON:  {"greeting":"Hello World"}
loo_Json.UpdateString("greeting","Hello World")

//  This URL echoes the contents of the request body, letting us view it.
ls_Url = "https://chilkatsoft.com/echo_request_body.asp"

//  Send a POST with the JSON in the HTTP request body.
loo_Resp = create oleobject
li_rc = loo_Resp.ConnectToNewObject("Chilkat.HttpResponse")

li_Success = loo_Http.HttpJson("POST",ls_Url,loo_Json,"application/json",loo_Resp)
if li_Success = 0 then
    Write-Debug loo_Http.LastErrorText
    destroy loo_Http
    destroy loo_Json
    destroy loo_Resp
    return
end if

Write-Debug "Response Status Code: " + string(loo_Resp.StatusCode)

//  Examine the request we sent.
//  The LastHeader property stores the HTTP request header of the most recent request sent,
//  and in this instance, the response body mirrors the request body.
Write-Debug loo_Http.LastHeader
Write-Debug loo_Resp.BodyStr

//  Output:

//  POST /echo_request_body.asp HTTP/1.1
//  Host: chilkatsoft.com
//  Accept: */*
//  Accept-Encoding: gzip
//  Content-Type: application/json
//  Content-Length: 26
//  
//  {"greeting":"Hello World"}


destroy loo_Http
destroy loo_Json
destroy loo_Resp