AFNetworking - How can I PUT and POST raw data without using a key value pair?
Solution 1
Hejazi's answer is simple and should work great.
If, for some reason, you need to be very specific for one request - for example, if you need to override headers, etc. - you can also consider building your own NSURLRequest.
Here's some (untested) sample code:
// Make a request...
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:myURL];
// Generate an NSData from your NSString (see below for link to more info)
NSData *postBody = [NSData base64DataFromString:yourBase64EncodedString];
// Add Content-Length header if your server needs it
unsigned long long postLength = postBody.length;
NSString *contentLength = [NSString stringWithFormat:@"%llu", postLength];
[request addValue:contentLength forHTTPHeaderField:@"Content-Length"];
// This should all look familiar...
[request setHTTPMethod:@"POST"];
[request setHTTPBody:postBody];
AFHTTPRequestOperation *operation = [client HTTPRequestOperationWithRequest:request success:success failure:failure];
[client enqueueHTTPRequestOperation:operation];
The NSData category method base64DataFromString
is available here.
Solution 2
You can use multipartFormRequestWithMethod
method as following:
NSURLRequest *request = [self multipartFormRequestWithMethod:@"PUT" path:path parameters:parameters constructingBodyWithBlock:^(id <AFMultipartFormData> formData) {
[formData appendString:<yourBase64EncodedString>]
}];
AFHTTPRequestOperation *operation = [client HTTPRequestOperationWithRequest:request success:success failure:failure];
[client enqueueHTTPRequestOperation:operation];
Solution 3
Here you have an example sending a raw json:
NSDictionary *dict = ...
NSError *error;
NSData *dataFromDict = [NSJSONSerialization dataWithJSONObject:dict
options:NSJSONWritingPrettyPrinted
error:&error];
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
NSMutableURLRequest *req = [[AFJSONRequestSerializer serializer] requestWithMethod:@"POST" URLString:YOUR_URL parameters:nil error:nil];
req.timeoutInterval = 30;
[req setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[req setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[req setValue:IF_NEEDED forHTTPHeaderField:@"Authorization"];
[req setHTTPBody:dataFromDict];
[[manager dataTaskWithRequest:req completionHandler:^(NSURLResponse * _Nonnull response, id _Nullable responseObject, NSError * _Nullable error) {
if (!error) {
NSLog(@"%@", responseObject);
} else {
NSLog(@"Error: %@, %@, %@", error, response, responseObject);
}
}] resume];
Solution 4
NSData *data = someData;
NSMutableURLRequest *requeust = [NSMutableURLRequest requestWithURL:
[NSURL URLWithString:[self getURLWith:urlService]]];
[reqeust setHTTPMethod:@"PUT"];
[reqeust setHTTPBody:data];
[reqeust setValue:@"application/raw" forHTTPHeaderField:@"Content-Type"];
NSURLSessionDataTask *task = [manager uploadTaskWithRequest:requeust fromData:nil progress:^(NSProgress * _Nonnull uploadProgress) {
} completionHandler:^(NSURLResponse * _Nonnull response, id _Nullable responseObject, NSError * _Nullable error) {
}];
[task resume];
Related videos on Youtube
Nick
Software developer in the Boise, Idaho area. Interests: C#, C\C++, Objective-C Client-side scripting Design architecture, Patterns
Updated on November 02, 2022Comments
-
Nick over 1 year
I am trying to make an HTTP PUT request using
AFNetworking
to create an attachment in a CouchDB server. The server expects a base64 encoded string in the HTTP body. How can I make this request without sending the HTTP body as a key/value pair usingAFNetworking
?I began by looking at this method:
- (void)putPath:(NSString *)path parameters:(NSDictionary *)parameters success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure;
But here the parameters are to be of type:
NSDictionary
. I just want to send a base64 encoded string in the HTTP body but not associated with a key. Can someone point me to the appropriate method to use? Thanks! -
CRDave about 10 yearsCan u help me here : stackoverflow.com/questions/22071188/…
-
Aaron Brager almost 10 years@ChitraKhatri It's an
AFHTTPClient
instance (from AFNetworking 1, it no longer exists in AFNetworking 2) -
Myxtic almost 10 yearsFor AFNetworking 2, you can use the following solution as a reference: stackoverflow.com/a/20812002/1048331