2017-11-08 5 views
0

いくつかのネストされたJSONを含む画像を含むマルチパートフォームデータを送信しようとしていますが、 リクエストに失敗しました:受け入れられないcontent-type:text/htmlエラー。 私が以下に示すように使用したコードは、何かが見つからない場合にお知らせください。要求を返すAfnetworkingのmultipart/formdataがios 11で失敗しましたが、ios 10で正常に動作しています

// AFNetworkingマルチパート

func AFhttpPost(url:String,postData:NSMutableDictionary, completionHandler:(result: NSMutableDictionary,error:NSString) -> Void){ 

    print("postData \(postData)") 

    var dataReceived:NSDictionary? 
    var mutableData:NSMutableDictionary? 
    if(self.checkNetConnectivity()) 
    { 
     let afHTTP :AFJSONRequestSerializer = AFJSONRequestSerializer() 


     afHTTP.setValue("multipart/form-data;", forHTTPHeaderField: "Content-Type") 
     afHTTP.setValue("application/json", forHTTPHeaderField: "Accept") 



     let request: NSMutableURLRequest = afHTTP.multipartFormRequestWithMethod("POST", URLString: url, parameters: nil, constructingBodyWithBlock: {(formData: AFMultipartFormData) in 

      let allKeys:NSArray = postData.allKeys 


      for item in allKeys.enumerate(){ 
       let currentKey = item.element as! String 

       if(currentKey == "signature"){ 
        let signatureImage = postData.objectForKey("signature") as! UIImage 

        let imageData = UIImagePNGRepresentation(signatureImage)! 
        formData.appendPartWithFileData(imageData , name: "uploaded_file[]", fileName: "signature\(index)x.png", mimeType: "image/png") 


       } 
       else 
       { //if it is jsonobject and its NSData just send as it is otherwise encode it 
        let postDataBytes:NSData? 
        if(postData.valueForKey(currentKey)! .isKindOfClass(NSData)){ 
         postDataBytes = postData.valueForKey(currentKey)! as? NSData 

         print("postDataBytes: \(currentKey):\(postDataBytes)") 

        }else{ 
         postDataBytes = postData.valueForKey(currentKey)!.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion:true)! 

         print("postDataBytes: \(currentKey):\(postDataBytes)") 

        } 
        formData.appendPartWithFormData(postDataBytes!, name: currentKey) 

       } 

      } 

      print("formFinalData \(formData)") 


      }, error: nil) 

     let accessToken = SingleTon().getAccessToken() 
     let authorisationheader = "\(accessToken)" 
     request.setValue(authorisationheader, forHTTPHeaderField: "Authorization") 

     let config = NSURLSessionConfiguration.defaultSessionConfiguration() 
     config.URLCache = nil 
     config.URLCredentialStorage = nil 
     // let session = NSURLSession(configuration: config) 




     let managerS : AFURLSessionManager = AFURLSessionManager.init(sessionConfiguration:config) 

     managerS.responseSerializer = AFJSONResponseSerializer() 




     let uploadTask = managerS.uploadTaskWithStreamedRequest(request, progress: nil) { (response, responseObject, error) -> Void in 

      do 
      { print("data \(responseObject)") 
       if(responseObject != nil){ 
        //dataReceived = try NSJSONSerialization.JSONObjectWithData(responseObject! as! NSData, options: []) as? NSMutableDictionary 
        dataReceived = responseObject! as? NSDictionary 

        mutableData = dataReceived!.mutableCopy() as? NSMutableDictionary 
        completionHandler(result:mutableData! ,error:"") 

       }else{ 
        completionHandler(result:[:],error:error!.localizedDescription) 


       } 

      } 
     } 
     uploadTask.resume() 
    }else{ 

     completionHandler(result: [:],error:ReapConectivityStatus) 
    } 


} 

答えて

0

これは、サーバーではなく、すでにサポートされているタイプの "text/htmlのを" 送っていることを意味しています。私のソリューションは、AFURLResponseSerializationクラスで設定されたacceptableContentTypesに "text/html"を追加することでした。 "acceptableContentTypes"を検索し、手動で@ "text/html"を追加してください。

+0

いいえ動作しません。 – adarshaU

関連する問題