2

I am trying to convert this curl call to Alamofire in Swift.

 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\"}"

And I figured this ...

let headers:HTTPHeaders = ["Authorization": "Bearer " + token2Save]
let moreheaders:Parameters = ["Dropbox-API-Arg": ["path":sourcePath]]

Alamofire.request("https://content.dropboxapi.com/2/files/download", parameters: moreheaders, encoding: URLEncoding(destination: .queryString), headers: headers).responseJSON { feedback in
        guard feedback.result.value != nil else {
            print("Error: did not receive data", print("request \(request) feedback \(feedback)"))
            return
        }

But of course it doesn't work, it crashes with ...

request (Function) feedback FAILURE: responseSerializationFailed(Alamofire.AFError.ResponseSerializationFailureReason.jsonSerializationFailed(Error Domain=NSCocoaErrorDomain Code=3840 "Invalid value around character 0." UserInfo={NSDebugDescription=Invalid value around character 0.}))
Error: did not receive data ()

This needs to be in the header, not the body.

user3069232
  • 8,587
  • 7
  • 46
  • 87
  • 1
    http://stackoverflow.com/questions/33518839/nsjsonserialization-error-code-3840-invalid-value-around-character-0 – Bista Sep 18 '16 at 18:17
  • Expected result is in which format? – Bista Sep 18 '16 at 18:17
  • 1
    http://stackoverflow.com/questions/32355850/alamofire-invalid-value-around-character-0 – Bista Sep 18 '16 at 18:18
  • Thanks UB, you were right on the mark; although my problem is unsolved, needed to use responseData at which point it tells me I haven't encoded the correct parameters!! will resubmit the question.... – user3069232 Sep 19 '16 at 08:52

1 Answers1

3
let headers:HTTPHeaders = ["Authorization": "Bearer " + token2Save]
let moreheaders:Parameters = ["Dropbox-API-Arg": ["path":sourcePath]]

Alamofire.request("https://content.dropboxapi.com/2/files/download", parameters: moreheaders, encoding: URLEncoding(destination: .queryString), headers: headers).responseJSON { feedback in
        guard feedback.result.value != nil else {
            print("Error: did not receive data", print("request \(request) feedback \(feedback)"))
            return

URLEncoding(destination: .queryString), headers: headers).responseJSON

Replace responseJSON with responseString and check your response from webservice.. it will lead to get the error line.

Saranjith
  • 11,242
  • 5
  • 69
  • 122