Chilkat2-Python
Chilkat2-Python
Send a REST Request using StringBuilder Bodies
See more REST Examples
Demonstrates Rest.FullRequestSb, which sends a complete request whose text body is read from a StringBuilder and stores the text response body in a second StringBuilder.
Background. Using StringBuilder objects for the request and response bodies avoids extra string conversions, which is helpful when working with large payloads.
Chilkat Chilkat2-Python Downloads
import sys
import chilkat2
success = False
rest = chilkat2.Rest()
bTls = True
bAutoReconnect = True
success = rest.Connect("example.com",443,bTls,bAutoReconnect)
if (success == False):
print(rest.LastErrorText)
sys.exit()
# FullRequestSb sends a request whose text body is read from a StringBuilder and stores the text
# response body in a second StringBuilder. This avoids extra string conversions for large bodies.
sbRequest = chilkat2.StringBuilder()
sbRequest.Append("{ \"query\": \"chilkat\" }")
rest.AddHeader("Content-Type","application/json")
sbResponse = chilkat2.StringBuilder()
success = rest.FullRequestSb("POST","/api/search",sbRequest,sbResponse)
if (success == False):
print(rest.LastErrorText)
sys.exit()
responseText = sbResponse.GetAsString()
if (sbResponse.LastMethodSuccess == False):
print(sbResponse.LastErrorText)
sys.exit()
print(responseText)