【发布时间】:2019-06-05 16:46:10
【问题描述】:
Swift 5,Xcode 版本 10.2.1 (10E1001)
大家好,如果能提供任何帮助,我将不胜感激。
我正在创建一个调用以将附件 (PNG) 发布到我的 POST 调用。我正在打电话给 ServiceNow。如果我在正文中使用与 PostMan 相同的键,则 Postman 中的调用可以正常工作。但是,下面的附件似乎很难处理。此示例中的图像是 PNG 资源。
为了比较,如果我在 Postman 中省略了附件,我会得到完全相同的错误消息。我认为图像的格式不正确...
提前谢谢...
我从 ServiceNow 收到以下错误:
{
error = {
detail = "<null>";
message = "Failed to create the attachment. File part might be missing in the request.";
};
status = failure;
}
这是我的代码:
func createDataBody() -> Data {
let newLine = "\r\n"
let twoNewLines = newLine + newLine
let boundary = "----------------------------\(UUID().uuidString)" + newLine
var body = Data()
let stringEncoding = String.Encoding.utf16
body.append(boundary.data(using: stringEncoding)!)
let table_name = "Content-Disposition: form-data; name=\"table_name\"" + twoNewLines
body.append(table_name.data(using: stringEncoding)!)
//incident
body.append("incident".data(using: stringEncoding)!)
//new line
body.append(newLine.data(using: stringEncoding)!)
//boundary
body.append(boundary.data(using: stringEncoding)!)
let table_sys_id = "Content-Disposition: form-data; name=\"table_sys_id\"" + twoNewLines
body.append(table_sys_id.data(using: stringEncoding)!)
//ba931ddadbf93b00f7bbdd0b5e96193c
body.append("ba931ddadbf93b00f7bbdd0b5e96193c".data(using: stringEncoding)!)
//new line
body.append(newLine.data(using: stringEncoding)!)
//boundary
body.append(boundary.data(using: stringEncoding)!)
let file = "Content-Disposition: form-data; name=\"file\"; filename=\"Artboard@1x.png\"" + newLine
body.append(file.data(using: stringEncoding)!)
let type = "Content-Type: image/png" + twoNewLines
body.append(type.data(using: stringEncoding)!)
//new line
body.append(newLine.data(using: stringEncoding)!)
let img = #imageLiteral(resourceName: "Artboard@1x")
if let fileContent = img.pngData() {
body.append(fileContent)
}
//new line
body.append(newLine.data(using: stringEncoding)!)
body.append("--\(UUID().uuidString)--".data(using: stringEncoding)!)
print(String(data: body, encoding: .utf16)!)
return body
}
这是身体的样子,省略了图像数据:
----------------------------F2152BF1-CE54-4E86-B8D0-931FA36F7C36
Content-Disposition: form-data; name="table_name"
incident
----------------------------F2152BF1-CE54-4E86-B8D0-931FA36F7C36
Content-Disposition: form-data; name="table_sys_id"
ba931ddadbf93b00f7bbdd0b5e96193c
----------------------------F2152BF1-CE54-4E86-B8D0-931FA36F7C36
Content-Disposition: form-data; name="file"; filename="Artboard@1x.png"
Content-Type: image/png
.....
----------------------------F2152BF1-CE54-4E86-B8D0-931FA36F7C36
这是标题调用
func addAttachmentToIncident() {
let passwordString = "\(userNameTextField.text!):\(passwordTextField.text!)"
let passwordData = passwordString.data(using: String.Encoding.utf8)
let base64EncodedCredential = passwordData?.base64EncodedString(options: Data.Base64EncodingOptions.lineLength76Characters)
let boundary = generateBoundaryString()
let headers = [
"authorization": "Basic " + base64EncodedCredential!,
"cache-control": "no-cache",
"Accept": "application/json",
"content-type": "multipart/form-data; boundary=--\(boundary)"
]
guard let url = URL(string: "https://xxx.service-now.com/api/now/attachment/upload") else {
return
}
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.allHTTPHeaderFields = headers
let dataBody = createDataBody(boundary: boundary)
request.httpBody = dataBody
let session = URLSession.shared
session.dataTask(with: request) { (data, response, error) in
if let response = response {
print(response)
}
if let data = data {
do {
let json = try JSONSerialization.jsonObject(with: data, options: [])
print(json)
} catch {
print(error)
}
}
}.resume()
} //addAttachmentToIncident
【问题讨论】:
-
我猜你想
POST到/now/attachment/upload对吗? -
是的。
https://xxx.service-now.com/api/now/attachment/upload" -
好的,然后仔细检查文件字段名,因为their example 使用
uploadFile,而不是file像您的Swift 代码。 -
当然我会仔细检查,但我在 PostMan 中使用
file并且它有效... -
好的。我在那里没有帐户,所以我无法测试它。我刚刚离开他们的
curl示例...
标签: swift post postman multipartform-data