You usually put Content-Disposition:form-data; name="field-name" as the part header for the text field. I tried to do as instructed which makes my body looks like this:
						body += "--\(boundary)\r\n"
						body += "Content-Disposition:form-data; name=\"image\""
						body += "\r\n\r\n\(base64Image ?? "")\r\n"
						body += "Content-Disposition:form-data; name=\"title\""
						body += "\r\n\r\n\(name)\r\n"
						body += "--\(boundary)--\r\n"
The request still works but the title of the picture is not defined
I think I did not mention it but the request I'm trying to use is the post image from the Imgur API
https://apidocs.imgur.com/#de179b6a-3eda-4406-a8d7-1fb06c17cb9c Edit: Also tried adding a boundary between to have something like this:
body += "--\(boundary)\r\n"
						body += "Content-Disposition:form-data; name=\"image\""
						body += "\r\n\r\n\(base64Image ?? "")\r\n"
						body += "--\(boundary)--\r\n"
						body += "Content-Disposition:form-data; name=\"title\""
						body += "\r\n\r\n\(name)\r\n"
						body += "--\(boundary)--\r\n"
but this is still not working
Edit2:
Had to change the way of putting the boundary to something like this:
body += "--\(boundary)\r\n"
which makes the body look like this:
body += "--\(boundary)\r\n"
						body += "Content-Disposition:form-data; name=\"image\""
						body += "\r\n\r\n\(base64Image ?? "")\r\n"
						body += "--\(boundary)\r\n"
						body += "Content-Disposition:form-data; name=\"title\""
						body += "\r\n\r\n\(name)\r\n"
						body += "--\(boundary)--\r\n"
Which is now working
Thanks again for your help and the time you took to answer my questions !
Post
Replies
Boosts
Views
Activity
I strongly recommend you not to use too short string such as "&" as boundary. I chose to generate a UID as my boundary
The bodyData is not in the right format for multipart/form-data.
I've been looking at the format of the form-data and based on what I saw on the tutorial I now have:
						var body = ""
						body += "--\(boundary)\r\n"
						body += "Content-Disposition:form-data; name=\"image\""
						body += "\r\n\r\n\(base64Image ?? "")\r\n"
						body += "; name=\"title\""
						body += "mycat\r\n"
						body += "--\(boundary)--\r\n"
						let postData = body.data(using: .utf8)
						request.httpBody = postData
The request works perfectly fine, the response I get is the following:
{"data":{"id":"wfoNb7C","title":null,"description":null,"datetime":1602920269,"type":"image\/png","animated":false,"width":275,"height":183,"size":90250,"views":0,"bandwidth":0,"vote":null,"favorite":false,"nsfw":null,"section":null,"account_url":null,"account_id":139373323,"is_ad":false,"in_most_viral":false,"has_sound":false,"tags":[],"ad_type":0,"ad_url":"","edited":"0","in_gallery":false,"deletehash":"fQm4439FpbjD3Ya","name":"","link":"https:\/\/i.imgur.com\/wfoNb7C.png"},"success":true,"status":200}
but the title of the image is not set despite I try to add it in my request
Any ideas on how to fix this problem ?
I chose iOS and then App but I don't have the files like "AppDelegate.swift" or "ViewControler.swift"
The tutorial I'm trying to follow is the one on developer.apple (https://developer.apple.com/library/archive/referencelibrary/GettingStarted/DevelopiOSAppsSwift/BuildABasicUI.html#//apple_ref/doc/uid/TP40015214-CH5-SW1)
If you have any idea of a better tutorial for beginner that I should follow I'd like to have the name