title: "Invalid Form Data" in file upload on slack API
body: <p>I'm working on a project where I need to upload files to channels in a slack workspace using slack's API. This is how I'm reading the file contents:</p>
  <pre class="lang-swift prettyprint-override"><code>func readFile(localFilePath: String) -&gt; Data? {
          var contents: Data
          let url = URL(fileURLWithPath: localFilePath)
          
          do {
              contents = try Data(contentsOf: url)
          } catch {
              return nil
          }
  
          return contents
      }
  </code></pre>
  <p>However, this is what I get on slack:</p>
  <p><a href="https://i.stack.imgur.com/rcIev.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/rcIev.png" alt="enter image description here" /></a></p>
  <p>I have also tried reading the file as a <code>String</code>, like this:</p>
  <pre class="lang-swift prettyprint-override"><code>contents = try String(contentsOf: url, encoding: .ascii)
  </code></pre>
  <p>This way slack appears to recognize it's a PDF, but it's blank (see image below).</p>
  <p><a href="https://i.stack.imgur.com/ncaux.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ncaux.png" alt="enter image description here" /></a></p>
  <p>If I add an explicit <code>Content-Type</code> header and set it to <code>multipart/form-data;</code>, I get an <code>invalid_form_data</code> error in response.</p>
  <p>Here's the request code:</p>
  <pre class="lang-swift prettyprint-override"><code>let reqHeaders: HTTPHeaders = [
              &quot;Authorization&quot;: &quot;&quot;,
              &quot;Accept&quot;: &quot;application/json&quot;,
              &quot;Content-Type&quot;: &quot;multipart/form-data;&quot;,
          ]
  let params: [String:Any] = [
              &quot;channels&quot;: channelSelections.joined(separator: &quot;,&quot;),
              &quot;content&quot;: fileData,
              &quot;initial_comment&quot;: messageText,
              &quot;filename&quot;: filename
          ]
          AF.request(url, method: .post, parameters: params, headers: reqHeaders).responseJSON { response in
              switch response.result {
              case .success:
                  if let data = response.data {
                      do {
                          let json = try JSON(data: data)
                          if json[&quot;ok&quot;].boolValue {
                              // some code
                          } else {
                              print(&quot;\n\n\n\(json)\n\n\n&quot;)
                          }
                      }
                      catch{
                          print(&quot;JSON Error&quot;)
                      }
                      
                  }
              case let .failure(error):
                  print(error)
              }
          }
  </code></pre>
  <p>What's the correct way to read the file and send as payload to slack's API?</p>