Stack Exchange
Alamofire failing to upload a file to Google Cloud Storage
I'm trying to upload a media file to GCS(Google Cloud Storage) via Alamofire. below is the code.
   let soundUrl = URL(fileURLWithPath: dirPaths.appending("/audio-file.wav"))
    
    let headers: HTTPHeaders = [
        "Content-Type": "audio/wav",
        "Authorization": "Bearer <MYTOKEN>"
    ]


    Alamofire.upload(
        multipartFormData: { multipartFormData in
            multipartFormData.append(soundUrl, withName: "name")
    },
        to: "https://www.googleapis.com/upload/storage/v1/b/<folder-name>/o?uploadType=media?&name=audio-file.wav",method: .post, headers: headers,
        encodingCompletion: { encodingResult in
            switch encodingResult {
            case .success(let upload, _, _):
                upload.responseString(completionHandler: { (response) in
                    print(response)
                })

            case .failure(let encodingError):
            print(encodingError)
           
            }

         }
    )

But it's not getting success.
the same thing I tried it in Postman. and it works fine.. I checked the post request from https://requestb.in both looks similar.
not sure where is the problem.
UPDATE -
Do I need to get authorization token for iOS? because I got the token from the terminal. and I put that in Header. I guess it's okay right?

Ca you please tell what error it throwing?

i tried with responseJson, responseData, responseString all doesnt give me the out put what server says.  responseJson - FAILURE: responseSerializationFailed(Alamofire.AFError.ResponseSerializationFailureReason.jsonSerializationFailed(Error Domain=NSCocoaErrorDomain Code=3840 "Invalid value around character 0." UserInfo={NSDebugDescription=Invalid value around character 0.}))

Since OAuth expiring like every hour we need to obtain it from device. and once i have the token i was able to upload the file from following code.
let soundUrl = URL(fileURLWithPath: dirPaths.appending("/audio-file.wav"))

let headers: HTTPHeaders = [
    "Content-Type": "audio/wave",
    "Authorization": "Bearer \(self.authorizationKey)"
]        
    Alamofire.upload(soundUrl, to: "https://www.googleapis.com/upload/storage/v1/b/<FOLDER>/o?uploadType=media&name=audio-file.wav",method: HTTPMethod.post, headers: headers).responseJSON{ response in
    print(response)
}


Hello I'm digging the doc without success to find out how to build this Authorization token to be able to upload and read from Google Cloud. How did you create this authkey?

You've set uploadType=media, but it looks like you're doing a multipart upload. If you want to upload just the media, don't do a multipart upload. If you do want to do a multipart upload, set uploadType=multipart. Getting a multipart upload to work will require that your upload has two parts, the first of which must be a JSON document describing the resource, and the second is the media.
These two upload mechanisms are described here: https://cloud.google.com/storage/docs/json_api/v1/how-tos/multipart-upload

so that mean i dont need to get the OAuth token again for iOS right? i thought that is the case. the one i took from Mac terminal will work for iOS device as well?
