Sample code for 30+ languages & platforms
Objective-C

Send a REST Request with a Binary Body

See more REST Examples

Demonstrates Rest.FullRequestBd, which sends a complete request whose binary body is read from a BinData and stores the text response body in a StringBuilder.

The file paths are relative to the application's current working directory. Absolute paths may also be used. Supply the paths appropriate to your own environment.

Background. FullRequestBd is used when the request payload is binary, such as an image or other file, while the response is textual (for example a JSON acknowledgement).

Chilkat Objective-C Downloads

Objective-C
#import <CkoRest.h>
#import <CkoBinData.h>
#import <CkoStringBuilder.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;
}

//  The file paths are relative to the application's current working directory.  Absolute paths
//  may also be used.  Supply the paths appropriate to your own environment.

//  FullRequestBd sends a request whose binary body is read from a BinData and stores the text
//  response body in a StringBuilder.
CkoBinData *bdRequest = [[CkoBinData alloc] init];
BOOL bLoaded = [bdRequest LoadFile: @"qa_data/image.png"];
if (bLoaded != YES) {
    NSLog(@"%@",@"Failed to load the request body file.");
    return;
}

[rest AddHeader: @"Content-Type" value: @"image/png"];

CkoStringBuilder *sbResponse = [[CkoStringBuilder alloc] init];
success = [rest FullRequestBd: @"PUT" uriPath: @"/api/images/1" binData: bdRequest responseBody: sbResponse];
if (success == NO) {
    NSLog(@"%@",rest.LastErrorText);
    return;
}

NSString *responseText = [sbResponse GetAsString];
if (sbResponse.LastMethodSuccess == NO) {
    NSLog(@"%@",sbResponse.LastErrorText);
    return;
}

NSLog(@"%@",responseText);