What if I had to attach a cookie with a post request? How should I do this?
NSURL *URL = [NSURL URLWithString:addAddressUrl];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL];
// Set cookie too
NSArray *cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookiesForURL:[NSURL URLWithString:@"<URL>"]];
NSDictionary *cookiesDictionary = [NSHTTPCookie requestHeaderFieldsWithCookies:cookies];
if(cookiesDictionary) {
[request setAllHTTPHeaderFields:cookiesDictionary];
}
How to attach this request with AFNetworking call? I have gone through the documents of AFNetworking but it doesn't explain how to set cookie in a request with its manager object.
And if I somehow attach this cookie into afnetworking files internally, still I am not able to upload image. I have tried two possible ways of it:
First way:
-(void)uploadPrescriptionImage :(UIImage *)imagePresc
{
// upload image here on the prescription
/*
Uploading a prescription
URL: <URL>
Params: <PARAMS>
Method: POST
*/
NSData *imageData = UIImageJPEGRepresentation(imagePresc, 1.0);
NSString *orderID = [[NSUserDefaults standardUserDefaults] valueForKey:@"orderId"]; // orderId
NSDictionary *parameters = @{@"docName":prescriptionCell.doctorNameTextField.text,@"patientName":prescriptionCell.patientNameTextField.text,@"orderId" : orderID};
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager POST:@"<URL>" parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData)
{
[formData appendPartWithFileData:imageData name:@"prescription" fileName:@"prescription" mimeType:@"image/jpeg"];
}
success:^(AFHTTPRequestOperation *operation, id responseObject)
{
NSLog(@"response is : %@",responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error)
{
NSLog(@"Error: %@ *****", [error description]);
}];
}
I have attached cookie in afnetworking method like below :
- (AFHTTPRequestOperation *)POST:(NSString *)URLString
parameters:(NSDictionary *)parameters
constructingBodyWithBlock:(void (^)(id <AFMultipartFormData> formData))block
success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure
{
NSMutableURLRequest *request = [self.requestSerializer multipartFormRequestWithMethod:@"POST" URLString:[[NSURL URLWithString:URLString relativeToURL:self.baseURL] absoluteString] parameters:parameters constructingBodyWithBlock:block];
NSArray *cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookiesForURL:[NSURL URLWithString:@"<URL>"]];
NSDictionary *cookiesDictionary = [NSHTTPCookie requestHeaderFieldsWithCookies:cookies];
if (cookiesDictionary) {
[request setAllHTTPHeaderFields:cookiesDictionary];
}
AFHTTPRequestOperation *operation = [self HTTPRequestOperationWithRequest:request success:success failure:failure];
[self.operationQueue addOperation:operation];
return operation;
}
Second way :
NSDictionary *parameters = @{@"docName":@"rr",@"patientName":@"tt",@"orderId" : @"1"};
NSString *URLString = @"<URL>";
//
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];
//
NSURL *URL = [NSURL URLWithString:URLString];
NSMutableURLRequest *request = [NSURLRequest requestWithURL:URL];
// Set cookie too
NSArray *cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookiesForURL:[NSURL URLWithString:@"<URL>"]];
NSDictionary *cookiesDictionary = [NSHTTPCookie requestHeaderFieldsWithCookies:cookies];
if (cookiesDictionary) {
[request setAllHTTPHeaderFields:cookiesDictionary];
}
//
// NSURL *filePath = [NSURL fileURLWithPath:@"file://path/to/image.png"];
NSURLSessionUploadTask *uploadTask = [manager uploadTaskWithRequest:request fromFile:[NSURL URLWithString:filePathOfImage] progress:nil completionHandler:^(NSURLResponse *response, id responseObject, NSError *error)
{
if (error) {
NSLog(@"Error: %@", error);
} else {
NSLog(@"Success: %@ %@", response, responseObject);
}
}];
[uploadTask resume];
}
But I don't know how to add parameters with this request. I prefer the second way.