How can I set cookies with AFNetworking 2.0 while uploading image with parameteres?

后端 未结 1 1907
夕颜
夕颜 2021-02-06 08:52

What if I had to attach a cookie with a post requst? How should I do this?

NSURL *URL = [NSURL URLWithString:addAddressUrl]; 
NSMutableURLRequest *request = [NS         


        
相关标签:
1条回答
  • 2021-02-06 09:25

    Was able to solve it..I have used it with only first way .. may be that can be used in both the ways :

    Here is my code of how i was able to upload an image with afnetworking 2.0 :

    NSString *fileName = [NSString stringWithFormat:@"Prescription%d.jpg", counter];
    
        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:@"http://staging.healthkartplus.com/webservices/prescription/upload" parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData){
            [formData appendPartWithFileData:imageData name:@"file" fileName:fileName mimeType:@"image/jpeg"];
    
        }success:^(AFHTTPRequestOperation *operation, id responseObject)
         {
             NSLog(@"response is :  %@",responseObject);
         }failure:^(AFHTTPRequestOperation *operation, NSError *error)
         {
             NSLog(@"Error: %@ *****", [error description]);
         }];
    

    The important part of this code is :

    [formData appendPartWithFileData:imageData name:@"file" fileName:fileName mimeType:@"image/jpeg"];
    

    Here in above line of code you need to specify exactly the name of the parameter and the type of the parameter (image). Here the "file" parameter must be passed to it (according to API which i got from my server side guys) and the type should be "image/jpeg"(or image/pdf or i think image/png). and you can pass any name for the fileName, here i am passing file name as : NSString *fileName = [NSString stringWithFormat:@"Prescription%d.jpg", counter];

    The only mistake i was doing is i was not specifying correct parameter for image , to summerise i should have sent it like :

    • parameter key : "file"
    • data : "file or image data"
    • filename : ".jpg" // NOTE : extension of image name must be there
    • mime type : ""
    0 讨论(0)
提交回复
热议问题