title: How to translate my Android code to Swift code for my Firebase function?
body: <p>From my research, there isn't much help translating Android code to Swift code.  With some help, we were able to translate or convert some of the code but it's not quite finished.  When I run the code, I get an error:</p>
  
  <pre><code>Response could not be serialized, input data was nil or zero length.
  
  responseSerializationFailed(reason: Alamofire.AFError.ResponseSerializationFailureReason.inputDataNilOrZeroLength)
  </code></pre>
  
  <p>Android code needing converting to Swift code:</p>
  
  <pre><code>public static final MediaType MEDIA_TYPE = MediaType.parse("application/json");
  ProgressDialog progress;
  
  private void payoutRequest() {
  
  progress = new ProgressDialog(this);
  progress.setTitle("Processing your payout ...");
  progress.setMessage("Please Wait .....");
  progress.setCancelable(false);
  progress.show();
  
  // HTTP Request ....
  final OkHttpClient client = new OkHttpClient();
  
  // in json - we need variables for the hardcoded uid and Email
  JSONObject postData = new JSONObject();
  
  try {
      postData.put("uid", FirebaseAuth.getInstance().getCurrentUser().getUid());
      postData.put("email", mPayoutEmail.getText().toString());
  
  } catch (JSONException e) {
      e.printStackTrace();
  }
  
  // Request body ...
  RequestBody body = RequestBody.create(MEDIA_TYPE, postData.toString());
  
  // Build Request ...
  final Request request = new Request.Builder()
          .url("https://us-central1-myapp.cloudfunctions.net/payout")
          .post(body)
          .addHeader("Content-Type", "application/json")
          .addHeader("cache-control", "no-cache")
          .addHeader("Authorization", "Your Token")
          .build();
  
  client.newCall(request).enqueue(new Callback() {
      @Override
      public void onFailure(Call call, IOException e) {
          // something went wrong right off the bat
          progress.dismiss();
      }
  
      @Override
      public void onResponse(Call call, Response response) throws IOException {
          // response successful ....
          // refers to response.status('200') or ('500')
          int responseCode = response.code();
          if (response.isSuccessful()) {
              switch(responseCode) {
                  case 200:
                      Snackbar.make(findViewById(R.id.layout),
                              "Payout Successful!", Snackbar.LENGTH_LONG)
                              .show();
                      break;
  
                  case 500:
                      Snackbar.make(findViewById(R.id.layout),
                              "Error: no payout available", Snackbar
                                      .LENGTH_LONG).show();
                      break;
  
                  default:
                      Snackbar.make(findViewById(R.id.layout),
                              "Error: couldn't complete the transaction",
                              Snackbar.LENGTH_LONG).show();
                      break;
              }
  
          } else {
              Snackbar.make(findViewById(R.id.layout),
                      "Error: couldn't complete the transaction",
                      Snackbar.LENGTH_LONG).show();
          }
  
          progress.dismiss();
      }
  });
  }
  </code></pre>
  
  <p>Swift code used from the above Android code:</p>
  
  <pre><code>let url = "https://us-central1-myapp.cloudfunctions.net/payout"
  let headers: HTTPHeaders = [
      "Content-Type": "application/json",
      "cache-control": "Your Token"]
  
  Alamofire.request(url, method: .get, headers: headers).validate().responseJSON { (response) in
       switch response.result {
          case .success(let value):
              // you fall here once you get 200 success code, because you use .validate() when you make call.
              print(value)
              // parse your JSON here.
              let parameters : [String: Any] =
                  ["uid": FIRAuth.auth()!.currentUser!.uid,
                   "email": self.paypalEmailText.text!]
  
              let postData = try! JSONSerialization.data(withJSONObject: parameters, options: [])
  
          case .failure(let error):
              if response.response?.statusCode == 500 {
                  print("Error no payout available")
                  print(error.localizedDescription)
              } else {
                  print("Error: couldn't complete the transaction")
                  print(error.localizedDescription)
              }
          }
  }
  </code></pre>
  
  <p>How can I convert the Android code into the Swift code or discover what it is I am doing wrong? This code is used to post to the function I have created for Firebase.</p>
  
  <h2>Edit</h2>
  
  <p>With the help of supplied code in this post, I was able to come up with this code but it is still coming up with the same error:</p>
  
  <pre><code>===========Error===========
  Error Code: 4
  Error Messsage: Response could not be serialized, input data was nil or zero length.
  response FAILURE: responseSerializationFailed(reason: Alamofire.AFError.ResponseSerializationFailureReason.inputDataNilOrZeroLength)
  </code></pre>
  
  <p><strong>updated swift code</strong></p>
  
  <pre><code>let url = "https://us-central1-myapp.cloudfunctions.net/payout"
      let headers: HTTPHeaders = [
          "Content-Type": "application/json",
          "cache-control": "Your Token"]
  
      let params : [String: Any] = [
          "uid": FIRAuth.auth()!.currentUser!.uid,
          "email": self.paypalEmailText.text!]
  
      Alamofire.request(url, method: .post, parameters: params, encoding: JSONEncoding.default, headers: headers).validate().responseJSON { (response) in
           switch response.result {
              case .success(let JSON):
                 print("Success with JSON: \(JSON)")
                 if (response.result.value as? [String:AnyObject]) != nil {
                  // Access your response here
                  print(response.result.value!)
                 }
  
              break
  
              case .failure(let error):
                  if response.response?.statusCode == 500 {
                      print("Error no payout available")
                      print(print("Request failed with error: \(error.localizedDescription)"))
  
                  } else {
                      print("Error: couldn't complete the transaction")
                      print("\n\n===========Error===========")
                      print("Error Code: \(error._code)")
                      print("Error Messsage: \(error.localizedDescription)")
  
                  }
              }
  
           print("response \(response)")
      }
  </code></pre>
  
  <p><strong>EDIT #2</strong></p>
  
  <p>I edited my method:</p>
  
  <pre><code>let url = "https://us-central1-myapp.cloudfunctions.net/payout"
  
      let headers: HTTPHeaders = [
          "cache-control": "no-cache",
          "Authorization": "Your Token",
          "Content-Type": "application/json"]
  
      let parameters : [String: Any] = [
              "uid": uid,
              "email": self.paypalEmailText.text!
      ]
  
      Alamofire.request(url, method: .post, parameters: parameters, encoding: JSONEncoding.default, headers: headers).validate(statusCode: 200..&lt;600).responseJSON { (response) in
  
          print("Request: \(String(describing: response.request))") // original url request
          print("Result: \(response.result)") // response serialization result
  
          if response.response?.statusCode == 200 {
  
              print("Success with JSON: \(String(describing: response.result.value))")
  
          } else {
              let error = (response.result.value  as? [[String : AnyObject]])
              print(error as Any)
          }
  
          print("response \(response)")
      }
  </code></pre>
  
  <p>The response and print outs are:</p>
  
  <pre><code>Request: Optional(https://us-central1-myapp.cloudfunctions.net/payout)
  Result: FAILURE
  Success with JSON: nil
  response FAILURE: responseSerializationFailed(reason: Alamofire.AFError.ResponseSerializationFailureReason.inputDataNilOrZeroLength)
  </code></pre>
  
  <p>Keep in mind, in my url, my app is not called "myapp" it is just there for protection.</p>