2016-09-19 7 views
0

スウィフトを使用してAlamofireにこれを送りたい...Alamofireのパラメータの符号化、辞書内の辞書

curl -X POST https://content.dropboxapi.com/2/files/download  
--header "Authorization: Bearer ab-xxx-x-x"  
--header "Dropbox-API-Arg: {\"path\": "/acme101/acmeX100/acmeX100.001.png\"}" 

しかし、あなたはコードにここを参照してください--header秒を取得する方法を見つけ出すことはできませんか?

JSON: 114 bytes Optional("Error in call to API function \"files/download\": Must provide HTTP header \"Dropbox-API-Arg\" or URL parameter \"arg\".") 

が、私は明らかではないそれを正しく提供しますが、異なる組み合わせで数回試みたが、...?

let subPart: NSDictionary = ["path": sourcePath] 
let headers:HTTPHeaders = ["Authorization": "Bearer " + token2Save, "Dropbox-API-Arg": String(describing: subPart)] 

    Alamofire.request("https://content.dropboxapi.com/2/files/download", method: .post, encoding: JSONEncoding.init(options: []), headers: headers).responseData(completionHandler: {feedback in 
+0

'print(String(describe:subPart))'、出力は何ですか? –

+0

'JSONEncoding.init(options:[])の代わりに' .JSON'を試してください –

+0

ここに表示されているサブパートの出力は "{ path =" /acme101/acmeX100/acmeX100.001.png "; }です。 Alamofireオプションとしてコンパイルします。注意Swift 3.0とAlamofire 4.0.0を使用しています。 – user3069232

答えて

0

ウレカ!!私にUBのアドバイスをくれたことに感謝します。解決策を見つけました。

let subPart: Dictionary = ["path":sourcePath] 
    do { 
     let data = try JSONSerialization.data(withJSONObject: subPart, options: []) 
     let dataString = String(data: data, encoding: .utf8) 
     headers = ["Authorization": "Bearer " + token2Save, "Dropbox-API-Arg": dataString!] 
    } catch { 
     print("crunch") 
    } 

Alamofire.request("https://content.dropboxapi.com/2/files/download", method: .post, encoding: JSONEncoding.init(options: []), headers: headers).responseData(completionHandler: {feedback in 
     guard feedback.result.value != nil else { 
     print("Error: did not receive data", print("request \(request) feedback \(feedback)")) 
     return 
     } 
     guard feedback.result.error == nil else { 
      print("error calling POST on list_folder") 
      print(feedback.result.error) 
      return 
     } 
     if let JSON = feedback.result.value { 
      print("JSON: \(JSON)") 
      let dataString = String(data: JSON, encoding: .utf8) 
      print("JSON: \(JSON) \(dataString)") 
     } 
     if let IMAGE = feedback.result.value { 
      sharedDataAccess.fnData(index2seek: 0, fnData: feedback.result.value! as Data) 
      NotificationCenter.default.post(name: Notification.Name("previewPane"), object: nil, userInfo: nil) 
     } 
    })