Phaxio API 创建传真并使用可怕的 multipart/form-data 编码类型

Phaxio API Create Fax and using the dreaded multipart/form-data encoding type

我正在放慢 Swift 到 Phaxio 集 API 的接口的速度。我已经弄清楚了一些基本的 GET 功能,但最难的是创建传真功能。这是我第一次尝试创建 multipart/form-data POST 并将二进制文件上传到传真服务器。我想我很接近了。我正在努力正确传递我的凭据和传真号码,同时还包括多部分格式的文件。刚才,我能够处理从服务器返回的文件上传响应 "bad Message"。现在我陷入了下面的错误。任何帮助将不胜感激。

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)."}

已解决!信不信由你,通过将凭据 API_KEY、API_SECRET 和 phone 号码附加到 URL 字符串,Phaxio 接受了消息并将传真排队等待发送。