PowerBuilder
PowerBuilder
curl POST with JSON Input and JSON Output
See more CURL Examples
Demonstrates running a simple curl command with JSON input and JSON output.Chilkat PowerBuilder Downloads
integer li_rc
integer li_Success
oleobject loo_SbTargetCurl
oleobject loo_HttpCurl
oleobject loo_ResponseJson
integer li_StatusCode
string ls_TargetCurl
li_Success = 0
// Run the following curl command
// curl -X POST https://httpbin.org/post \
// -H "Content-Type: application/json" \
// -d '{
// "title": "foo",
// "body": "bar",
// "userId": 1
// }'
// The backslashes at the end of lines are not required. Chilkat ignores them if present.
loo_SbTargetCurl = create oleobject
li_rc = loo_SbTargetCurl.ConnectToNewObject("Chilkat.StringBuilder")
if li_rc < 0 then
destroy loo_SbTargetCurl
MessageBox("Error","Connecting to COM object failed")
return
end if
loo_SbTargetCurl.AppendLn(" curl -X POST https://httpbin.org/post ~")
loo_SbTargetCurl.AppendLn(" -H ~"Content-Type: application/json~" ~")
loo_SbTargetCurl.AppendLn(" -d '{")
loo_SbTargetCurl.AppendLn(" ~"title~": ~"foo~",")
loo_SbTargetCurl.AppendLn(" ~"body~": ~"bar~",")
loo_SbTargetCurl.AppendLn(" ~"userId~": 1")
loo_SbTargetCurl.AppendLn(" }'")
loo_HttpCurl = create oleobject
li_rc = loo_HttpCurl.ConnectToNewObject("Chilkat.HttpCurl")
// Run the curl command.
li_Success = loo_HttpCurl.DoYourThing(loo_SbTargetCurl.GetAsString())
if li_Success = 0 then
Write-Debug loo_HttpCurl.LastErrorText
destroy loo_SbTargetCurl
destroy loo_HttpCurl
return
end if
loo_ResponseJson = create oleobject
li_rc = loo_ResponseJson.ConnectToNewObject("Chilkat.JsonObject")
loo_ResponseJson.EmitCompact = 0
loo_HttpCurl.GetResponseJson(loo_ResponseJson)
li_StatusCode = loo_HttpCurl.StatusCode
Write-Debug "response status code: " + string(li_StatusCode)
Write-Debug loo_ResponseJson.Emit()
// Output:
// response status code: 200
// {
// "args": {},
// "data": "{\r\n \"title\": \"foo\",\r\n \"body\": \"bar\",\r\n \"userId\": 1\r\n }",
// "files": {},
// "form": {},
// "headers": {
// "Content-Length": "96",
// "Content-Type": "application/json",
// "Host": "httpbin.org",
// "X-Amzn-Trace-Id": "Root=1-69e8db8b-459b3bdf7b7a3bc749184968"
// },
// "json": {
// "body": "bar",
// "title": "foo",
// "userId": 1
// },
// "origin": "123.222.222.222",
// "url": "https://httpbin.org/post"
// }
// ----------------------------------------------------------------------------------
// Another example:
// curl -X POST https://postman-echo.com/post \
// -H "Content-Type: application/json" \
// -d '{"foo":"bar"}'
ls_TargetCurl = "curl -X POST https://postman-echo.com/post -H ~"Content-Type: application/json~" -d '{~"foo~":~"bar~"}'"
// Run the curl command.
li_Success = loo_HttpCurl.DoYourThing(ls_TargetCurl)
if li_Success = 0 then
Write-Debug loo_HttpCurl.LastErrorText
destroy loo_SbTargetCurl
destroy loo_HttpCurl
destroy loo_ResponseJson
return
end if
loo_HttpCurl.GetResponseJson(loo_ResponseJson)
li_StatusCode = loo_HttpCurl.StatusCode
Write-Debug "response status code: " + string(li_StatusCode)
Write-Debug loo_ResponseJson.Emit()
// Output:
// response status code: 200
// {
// "args": {},
// "data": {
// "foo": "bar"
// },
// "files": {},
// "form": {},
// "headers": {
// "host": "postman-echo.com",
// "content-length": "13",
// "content-type": "application/json",
// "x-forwarded-proto": "https",
// "accept-encoding": "gzip, br"
// },
// "json": {
// "foo": "bar"
// },
// "url": "https://postman-echo.com/post"
// }
destroy loo_SbTargetCurl
destroy loo_HttpCurl
destroy loo_ResponseJson