Swift Phaxio API创建传真并使用可怕的多部分/表单数据编码类型

Swift Phaxio API创建传真并使用可怕的多部分/表单数据编码类型,swift,api,urlsession,Swift,Api,Urlsession,我正在放缓构建一个到Phaxio API集的快速接口。我已经找到了一些基本的GET函数,但最难的是createfax函数。这是我第一次尝试创建多部分/表单数据POST并将二进制文件上载到传真服务器。我想我很接近了。我正在努力正确地传递我的凭证和传真号码,同时还以多部分格式包含该文件。刚才,我可以通过“坏消息”的反应,我从服务器上传文件回来。现在我陷入了下面的错误。任何帮助都将不胜感激 import Cocoa // This program uses the phaxio API to sen

我正在放缓构建一个到Phaxio API集的快速接口。我已经找到了一些基本的GET函数,但最难的是createfax函数。这是我第一次尝试创建多部分/表单数据POST并将二进制文件上载到传真服务器。我想我很接近了。我正在努力正确地传递我的凭证和传真号码,同时还以多部分格式包含该文件。刚才,我可以通过“坏消息”的反应,我从服务器上传文件回来。现在我陷入了下面的错误。任何帮助都将不胜感激

import Cocoa

// This program uses the phaxio API to send a fax (at least thats the intent). Shout out to Ignacio Nieto Carvajal's very helpful Networking in Swift: The Complete Guide on www.digialleaves.com to help me piece this together. //

// handy function to create unique identifier to define the multi-part boundaries
func generateBoundaryString() -> String {
   return "Boundary-\(UUID().uuidString)"
}

var body = Data()
var parameter = ""

// Open file, read contents into buffer - This works great, so easy in SWIFT!
let fileMgr = FileManager.default
var sourceFile = "/tmp/test.pdf"
let databuffer = fileMgr.contents(atPath: sourceFile)

// post the user, pass and faxnumber
let boundary = generateBoundaryString()

parameter.append("--\(boundary)\r\n")
parameter.append(contentsOf: "Content-Disposition: form-data; name=\"api_key\"; value=\"cn577fcvrcrjuj9v8\"\r\n")

parameter.append("--\(boundary)\r\n")
parameter.append(contentsOf: "Content-Disposition: form-data; name=\"api_secret\"; value=\"ciwx0sc7owqv4gzg\"\r\n")

parameter.append("--\(boundary)\r\n")
parameter.append(contentsOf: "Content-Disposition: form-data; name=\"to\"; value=\"5555555555\"\r\n")

parameter.append("--\(boundary)\r\n")
parameter.append(contentsOf: "Content-Disposition: form-data; name=\"file\"; filename=\"\(sourceFile)\"\r\n")
parameter.append(contentsOf: "Content-Type: application/PDF\r\n\r\n")

// Create boundary around file contents
body.append(contentsOf: parameter.utf8)

// Add binary contents of file
body.append((databuffer ?? nil)!)

body.append(contentsOf: "\r\n".utf8)
body.append(contentsOf: "--\(boundary)--\r\n".utf8)

// Initialize our URL & Request
let baseURL = "https://api.phaxio.com/v2/faxes"
let session = URLSession.shared
let url = URL(string: baseURL)!
var request = URLRequest(url: url)

//Define request method & set header values
request.httpMethod = "POST"
request.setValue("multipart/form-data; boundary=\(boundary)", forHTTPHeaderField: "Content-Type")
request.setValue("application/json", forHTTPHeaderField: "Accept")

// Initialize HTTP Request Body
request.httpBody = body

URLSession.shared.dataTask(with: request) { (data, response, error) in
    if let error = error {
        print("error took Place\(error)")
        return
    }

    if let data = data, let dataString = String(data: data, encoding: .utf8) {
        print("response data string: \(dataString)")

    }

}.resume()


This is the Response I'm getting from the Server:

{"success":false, "message": "You must provide API credentials for this operation (HTTP auth or api_key and api_secret)."}


解决了!信不信由你,通过在URL字符串中添加凭证API_密钥、API_密钥和电话号码,Phaxio接受了消息,并将传真排队等待发送