I am currently working on a program to post images to Mastodon, but I have used the one in the post below as a reference, but
http://qiita.com/aryzae/items/8c16bc456588c1251f48#_reference-32d9e31807ff872c27f8
When I send it, I check the amount of data of 85406 bytes, but the reception is displayed as 0 bytes, and I am worried.
Normally, there should be some reception in json format...
I looked at the API list of my home GitHub (translated by Chrome), and I am having trouble solving it.
Thank you for your cooperation.
US>func testToot(){ let session = URLSession.shared let i = Item() let url = URL (string: "https://pawoo.net/api/v1/media")! let param: [String: String] = ["access_token": i.getItem(clum: "access_token") as!String] let image : Data = UIImageJPEGRepresentation (UIImage(named: "a.jpg")!, 0.8!as Data let boundary = "Boundary -\ (NSUUID().uuuidString)" var req = URLRequest(url:url) req.httpMethod="POST" req.addValue("application/json", for HTTPHeaderField:"Content-Type") req.addValue("multipart/form-data; boundary=\(boundary)" for HTTPHeaderField: "Content-Type") req.httpBody=createBody(_param:param,_key:"file", imageData:image,_boundary:boundary) let task = session.dataTask(with:req, completionHandler: {data, response, error in do { print(response!) } }) task.resume() } func createBody(_param:[String:String],_key:String, imageData:Data,_boundary:String)->(Data){ let file="a.jpg" let type="image/jpeg" varbody=Data(); for(key,val)in_param{ body.append("--`\(_boundary)\r\n") body.append("Content-Disposition: form-data; name=\(key)") body.append("\(val)\r\n") } body.append("--(_boundary)\r\n") body.append("Content-Disposition: form-data; name=\"\(_key)\"; filename=\"\(file)\"\r\n") body.append("Content-Type\(type)\r\n\r\n") body.append(imageData) body.append("r\n") body.append("----(_boundary)--\r\n") return body } } extension Data { mutating funcappend(_str:String){ let data = Data (str.utf8) return self.append(data) } }
Response Contents
{ URL: https://pawoo.net/api/v1/media } {status code:400, headers { "Content-Length" = 0; "Content-Type" = "text/html; charset=utf-8"; Date="Fri, 12 May 2017 12:44:39 GMT"; Vary = Origin; "x-request-id" = "aasf0bb-fb82-45c1-34d0-676ffc1aww15"; "x-runtime" = "0.155211"; } }swift3
Here are a few things that bothered me.
\r\n\r\n
.:
.\r
, but r
.
© 2024 OneMinuteCode. All rights reserved.