Sample code for 30+ languages & platforms
Objective-C

Capture the Composed REST Request for Debugging

See more REST Examples

Demonstrates Rest.GetLastDebugRequest, which copies the fully composed HTTP request (request line, headers, and body) into a BinData. The example builds a two-part multipart request so the captured output shows the multipart boundaries, part headers, and part bodies. This works when the DebugMode property was enabled while the request was composed.

Background. When DebugMode is enabled, Chilkat builds the request but does not transmit it, so GetLastDebugRequest can reveal the exact bytes that would be sent. Inspecting the composed request is valuable for diagnosing API problems.

Chilkat Objective-C Downloads

Objective-C
#import <CkoRest.h>
#import <CkoBinData.h>
#import <NSString.h>

BOOL success = NO;

CkoRest *rest = [[CkoRest alloc] init];
BOOL bTls = YES;
BOOL bAutoReconnect = YES;
success = [rest Connect: @"example.com" port: [NSNumber numberWithInt: 443] tls: bTls autoReconnect: bAutoReconnect];
if (success == NO) {
    NSLog(@"%@",rest.LastErrorText);
    return;
}

//  Enable debug mode so the fully composed HTTP request is captured instead of being sent.
rest.DebugMode = YES;

//  Build a multipart request with two parts.  The captured debug output will show the multipart
//  boundaries, part headers, and part bodies.
rest.PartSelector = @"1";
[rest AddHeader: @"Content-Type" value: @"text/plain"];
[rest AddHeader: @"Content-Disposition" value: @"form-data; name=\"field1\""];
success = [rest SetMultipartBodyString: @"value1"];
if (success == NO) {
    NSLog(@"%@",rest.LastErrorText);
    return;
}

rest.PartSelector = @"2";
[rest AddHeader: @"Content-Type" value: @"application/json"];
[rest AddHeader: @"Content-Disposition" value: @"form-data; name=\"metadata\""];
success = [rest SetMultipartBodyString: @"{ \"active\": true }"];
if (success == NO) {
    NSLog(@"%@",rest.LastErrorText);
    return;
}

//  Compose the multipart request.  In debug mode the request is built but not transmitted.
success = [rest SendReqMultipart: @"POST" uriPath: @"/api/upload"];
if (success == NO) {
    NSLog(@"%@",rest.LastErrorText);
    return;
}

//  Copy the composed request (request line, headers, and multipart body that would be sent) into a
//  BinData, then view it as text.
CkoBinData *bdRequest = [[CkoBinData alloc] init];
[rest GetLastDebugRequest: bdRequest];

NSString *requestText = [bdRequest GetString: @"utf-8"];
if (bdRequest.LastMethodSuccess == NO) {
    NSLog(@"%@",bdRequest.LastErrorText);
    return;
}

NSLog(@"%@",requestText);