Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I set cookies with AFNetworking 2.0 while uploading image with parameteres?
    primarykey
    data
    text
    <p>What if I had to attach a cookie with a post requst? How should I do this? </p> <pre><code>NSURL *URL = [NSURL URLWithString:addAddressUrl]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL]; // Set cookie too NSArray *cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookiesForURL:[NSURL URLWithString:@"&lt;URL&gt;"]]; NSDictionary *cookiesDictionary = [NSHTTPCookie requestHeaderFieldsWithCookies:cookies]; if(cookiesDictionary) { [request setAllHTTPHeaderFields:cookiesDictionary]; } </code></pre> <p>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.</p> <p>And iF i some how attach this cookie into afnetworking files internally , Still i am not able to upload image . I have tried two possible ways of it : </p> <p>First Way : </p> <pre><code>-(void)uploadPrescriptionImage :(UIImage *)imagePresc { // upload image here on the prescription /* Uploading a prescription URL: &lt;URL&gt; Params: &lt;PARAMS&gt; 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:@"&lt;URL&gt;" parameters:parameters constructingBodyWithBlock:^(id&lt;AFMultipartFormData&gt; 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]); }]; } </code></pre> <p>I have atttached cookie in afnetworking method like below : </p> <pre><code>- (AFHTTPRequestOperation *)POST:(NSString *)URLString parameters:(NSDictionary *)parameters constructingBodyWithBlock:(void (^)(id &lt;AFMultipartFormData&gt; 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:@"&lt;URL&gt;"]]; 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; } </code></pre> <p>Second way : </p> <pre><code> NSDictionary *parameters = @{@"docName":@"rr",@"patientName":@"tt",@"orderId" : @"1"}; NSString *URLString = @"&lt;URL&gt;"; // 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:@"&lt;URL&gt;"]]; 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]; } </code></pre> <p>But i dont know how do i add parameters with this request .. common Matt Thompson .. Please help me this is making frushtrated everyone .. I would like to prefer second way .. </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload