title: Why might I be getting an internal server error (500) on this http POST request
body: <p>I have been battling with this for ages. I am trying to upload an image to a server but keep getting a 500 error.</p>
  
  <p>Here is the code I use to get the image, base64 encode it, and then add it to a dictionary.</p>
  
  <pre><code> if let imageDataToUpload = UIImageJPEGRepresentation(selectedImage, 1.0) {
  
  
              let encodedImageData = imageDataToUpload.base64EncodedString(options: [])
  
              let extras = "data:image/jpeg;base64,"
  
              let postBody = [
                  "image": extras + encodedImageData,
                  "id": id,
                  "instruction": "1",
                  "ext": ""
              ]
  
              let endPoint = UrlFor.uploadPhoto
  
              ApiManager().apiPostImage(endPoint: endPoint, postBody: postBody, callBackFunc: handleResultOfUploadPhoto) }
  </code></pre>
  
  <p>And here is the code I use to perform the actual POST request. You'll notice I am trying to convert the post body to a JSON object.</p>
  
  <pre><code>func apiPostImage(endPoint: String, postBody: [String: Any]?, callBackFunc: @escaping (ResultOfApiCall) -&gt; Void) {
  
      // get the sessionKey
      guard let sessionKey = KeyChainManager().getSessionKey() else {
          print("error getting session key")
          AppDelegate().signUserOut()
          return
      }
  
      // create a url with above end point and check its a valid URL
      guard let url = URL(string: endPoint) else {
          print("Error: cannot create URL")
          return
      }
  
      // set up the request
      var urlRequest = URLRequest(url: url)
      urlRequest.httpMethod = "POST"
  
      if let postBody = postBody {
          do {
              try urlRequest.httpBody = JSONSerialization.data(withJSONObject: postBody, options: [])
          } catch {
              print("problems creating json body")
          }
      }
  
      // set up the header
      let config = URLSessionConfiguration.default
      config.httpAdditionalHeaders = [
          "Accept": "application/json",
          "apiKey": "0101010-0101010", // not the real apiKey
          "usrKey": sessionKey,
          "appInfo" : "appcode:1000|version:2.0.37",
          "Content-Type": "application/json"
      ]
  
      let session = URLSession(configuration: config)
  
      let task = session.dataTask(with: urlRequest, completionHandler:
          { (data: Data?, response: URLResponse?, error: Error?) -&gt; Void in
  
              let (noProblem, ResultOfCall) = self.checkIfProblemsWith(data: data, response: response, error: error)
  
              guard noProblem else {
                  callBackFunc(ResultOfCall)
                  return
              }
  
              let serializedData: [String:Any]
              do {
  
                  // safe bc checked for nil already.
                  serializedData = try JSONSerialization.jsonObject(with: data!, options: []) as! [String:Any]
  
              } catch  {
  
                  callBackFunc(ResultOfApiCall.errorWhileSerializingJSON)
                  return
              }
  
              // pass serialized data back using the callBackFunc
              callBackFunc(ResultOfApiCall.success(serializedData))
      })
  
      task.resume()
  }
  </code></pre>
  
  <p>The API is RESTful. I don't have access to the API error logs, but I am getting this error back from the API: </p>
  
  <pre><code>["codesc": GENERALERROR, "code": 5001, "msg": Conversion from string "Optional(1004007)" to type 'Double' is not valid.]
  </code></pre>