2017-03-16 2 views
0

Ich bin verwirrt mit Hinzufügen von Kopfzeilen in "AFNetworking 3.0". In der vorherigen Version gibt es eine Funktion, die als RequestSerializer bekannt ist. Wie kann ich einer Änderbaren Anfrage bestimmte Header hinzufügen?Wie Hinzufügen von Kopfzeilen zur Anfrage in AfNetworking 3.0?

{ 
NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"POST" URLString:@"http://example.com/" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) { 

NSString *path = [[NSBundle mainBundle] pathForResource:@"uploadedImage_3" ofType:@"jpg"]; 
[formData appendPartWithFileURL:[NSURL fileURLWithPath:path] name:@"file" fileName:@"filename.jpg" mimeType:@"image/jpeg" error:nil]; 
} 
error:nil]; 
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; 
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"]; 
[request addValue:@"8b10056e-59e6-4d2b-aa4f-b08f8afff80a" forHTTPHeaderField:@"session_key"]; 
[request addValue:@"0" forHTTPHeaderField:@"resume_key"]; 

AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]]; 

NSURLSessionUploadTask *uploadTask; 
uploadTask = [manager 
      uploadTaskWithStreamedRequest:request 
      progress:^(NSProgress * _Nonnull uploadProgress) { 
       // This is not called back on the main queue. 
       // You are responsible for dispatching to the main queue for UI updates 
       dispatch_async(dispatch_get_main_queue(), ^{ 
       //Update the progress view 
       [_progressView setProgress:uploadProgress.fractionCompleted]; 
       }); 
      } 
      completionHandler:^(NSURLResponse * _Nonnull response, id _Nullable responseObject, NSError * _Nullable error) { 
       if (error) { 
       NSLog(@"Error: %@", error); 
       } else { 
       NSLog(@"%@ %@", response, responseObject); 
       } 
      }]; 

[uploadTask resume]; 
} 

Ist das wie ich die Header zu dem MutableURLRequest hinzufügen?

NSLocalizedDescription = Request failed: unsupported media type (415)} 

Ich erhalte zur Zeit einen Fehler wie oben.

Antwort

1

Das Problem gefunden.

"Der Server verweigert die Bearbeitung der Anfrage, da die Entität der Anfrage in einem Format vorliegt, das von der angeforderten Ressource für die angeforderte Methode nicht unterstützt wird."

Mit anderen Worten, der Server unterstützt nicht Anwendung/Json.

entfernte ich nur diese beiden Zeilen und es funktionierte ...

[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; 
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"]; 
0

Ich schlage vor, Sie den Code wie diese zu schreiben,

func apiRequest(method:String, urlMethod:String, parametersDictionary:NSMutableDictionary, success:@escaping successDictionaryBlock, failure: @escaping failBlockErrorMessage){ 
     let requestUrl = @“www.requestUrl” 

     let headerString = "\("Bearer") \(request Token)” 
     let headers: HTTPHeaders = [ 
      "Authorization": headerString, 

      ] 

     Alamofire.request(requestUrl, method: .post, parameters: (parametersDictionary as NSDictionary) as? Parameters , encoding: JSONEncoding.default, headers: headers).responseJSON { response in 

      if(response.result.error == nil){ 

       if((response.response?.statusCode)! < 500){ 

        if(response.response?.statusCode == 200){ 

         if let JSON = response.result.value { 

          let dict = JSON as! NSDictionary 

          let status :Bool = dict["status"] as! Bool 

          if(status){ 
           success(dict) 

          }else{ 
           failure(dict["message"] as! String) 
          } 

         } 
        }else{ 
         failure("Something went wrong please try again") 
        } 
       }else{ 
        failure("Something went wrong please try again") 
       } 
      } 
     } 

    } 
Verwandte Themen