2017-05-07 7 views
0

私はAlamofireを初めて、Postmanから使用できるWebサービスを使用しようとしていますが、Alamofire経由では使用できません。郵便配達では、 "application/json"でHTTPヘッダキー "Accept"を設定することができ、Content-Type→application/jsonで応答が得られます。 charset = utf-8。 しかし、私がAlamofireを使って同じことをしようとすると、応答はxmlになるため、シリアル化は失敗します。 これには解決策がありますか?私はカスタムHTTPヘッダーを試しました& "Accept/Accept"キーが "application/json"に設定されているデフォルトセッションマネージャですが、意図した応答が得られません!ここにコードのサンプルがあります。Alamofire 4.4シリアライゼーションエラー、コンテンツタイプの問題

if let url = URL(string: "http://madinaty.demoday.us/api/1/lookup/stable/") { 
     var urlRequest = URLRequest(url: url) 
     urlRequest.httpMethod = HTTPMethod.get.rawValue 
     urlRequest.addValue("application/json", forHTTPHeaderField: "content-type") 
     urlRequest.addValue("application/json", forHTTPHeaderField: "accept") 

     let request = Alamofire.request(urlRequest) 
      .responseJSON{ response in 
       print("Request: \(String(describing: response.request))") // original URL request 
       print("---------------------------") 
       print("HTTP URL response: \(String(describing: response.response))") // HTTP URL response 
       print("---------------------------") 
       print("Data: \(String(describing: response.data))")  // server data 
       print("---------------------------") 
       print("Result of Reponse Serialization \(response.result)") // result of response serialization 
       print("---------------------------") 
       print("Error \(response.result.error)") // result of response serialization 
       print("---------------------------") 

       if let JSON = response.result.value { 
        print("JSON: \(JSON)") 

       } 

     } 

これは私が取得errorです:

オプション(Alamofire.AFError.responseSerializationFailed(Alamofire.AFError.ResponseSerializationFailureReason.jsonSerializationFailed(エラー文字 周り ドメイン= NSCocoaErrorDomainコード= 3840」無効な値を0 "。))))))))))

また、私はこれを試した:{NSDebugDescription =値が無効です値:

 let header = ["content-type" : "application/json"] 

    Alamofire.request("http://madinaty.demoday.us/api/1/lookup/stable/", 
         method: .get, 
         parameters: nil, 
         encoding: JSONEncoding.default, 
         headers: header).responseJSON { 
         response in 
         print(response.request) // original URL request 
         print(response.response) // HTTP URL response 
         print(response.data)  // server data 
         print(response.result) // result of response 

    } 

どちらも機能しません。

この作品でしたどちら:

   // get the default headers 
      var headers = Alamofire.SessionManager.defaultHTTPHeaders 
      // add your custom header 
      headers["Accept"] = "application/json" 

      // create a custom session configuration 
      let configuration = URLSessionConfiguration.default 
      // add the headers 
      configuration.httpAdditionalHeaders = headers 

      // create a session manager with the configuration 
      let sessionManager = Alamofire.SessionManager(configuration: configuration) 

      // make calls with the session manager 
      let request = sessionManager.request("http://madinaty.demoday.us/api/1/lookup/stable/") 
       .responseJSON { response in 
        debugPrint(response) 
        print("Result of Reponse Serialization \(response.result)") 
      } 
      debugPrint(request) 
+0

いくつかのコードを追加して、あなたが行ったことを見せてください。 – Simon

+0

@ Simonちょうど – Taher

+0

エラーオプション(Alamofire.AFError.responseSerializationFailed(Alamofire.AFError.ResponseSerializationFailureReason.jsonSerializationFailed(エラードメイン= NSCocoaErrorDomainコード= 3840「文字0の周辺で値が無効です」)UserInfo = {NSDebugDescription =文字0の値が無効です} )))) – Taher

答えて

0

は、カスタムセッションヘッダの設定を作成してみてくださいません:

var headers = Alamofire.SessionManager.defaultHTTPHeaders 
headers["content-type"] = "application/json" 

// custom session configuration 
let configuration = URLSessionConfiguration.default 
configuration.httpAdditionalHeaders = headers 

let sessionManager = Alamofire.SessionManager(configuration: configuration) 

そして、異なる試験の時間後の代わりに​​

+0

私はすでにこれも試してみました。編集した質問のバージョンを確認してください。 – Taher

+0

私はすでにこれも試しましたが、編集した質問のバージョンを確認してください – Taher

0

sessionManager.request(...)を使用して、あなたの要求を行います、私は最終的に問題の解決策を見つけた。

URLの末尾にあるスラッシュを削除するだけで問題が解決することが判明しました。

関連する問題