Saturday, April 1, 2023
Learning Code
  • Home
  • JavaScript
  • Java
  • Python
  • Swift
  • C++
  • C#
No Result
View All Result
  • Home
  • JavaScript
  • Java
  • Python
  • Swift
  • C++
  • C#
No Result
View All Result
Learning Code
No Result
View All Result
Home Swift

Easy multipart file upload for Swift

learningcode_x1mckf by learningcode_x1mckf
January 18, 2023
in Swift
0
Easy multipart file upload for Swift
74
SHARES
1.2k
VIEWS
Share on FacebookShare on Twitter


I imagine that you’ve got already heard in regards to the well-known multipart-data add method that everybody likes to add recordsdata and submit type knowledge, but when not, hopefully this text will assist you a bit bit to know these items higher.

You might also like

Swift Against Humanity – Hacking with Swift

Introducing MotionBar – the Animated Tab Bar Library

Introducing – AI Utils for macOS


Let’s begin with some principle. Don’t fret, it is only one hyperlink, about the multipart/form-data content type specification. To rapidly summarize it first I might wish to let you know a couple of phrases about how the HTTP layer works. In a nutshell, you ship some knowledge with some headers (give it some thought as a key-value consumer information object) to a given URL utilizing a technique and as a response you will get again a standing code, some headers and possibly some type of response knowledge too. πŸ₯œ


  • HTTP request = Technique + URL + Headers + Physique (request knowledge)
  • HTTP response = Standing code + Headers + Physique (response knowledge)

The request technique & URL is fairly simple, the fascinating half is while you specify the Content material-Sort HTTP header, in our case the multipart/form-data;boundary="xxx" worth means, that we will ship a request physique utilizing a number of components and we will use the “xxx” boundary string as a separator between the components. Oh, by the way in which every half can have it is personal kind and identify, we’ll use the Content material-Disposition: form-data; identify="field1" line to let the server learn about these fields, earlier than we really ship the precise content material worth.


That is greater than sufficient principle for now, let me snow you ways we are able to implement all of this utilizing Swift 5. Initially, we want to have the ability to append string values to a Information object, so we will lengthen Data type with an ‘append string utilizing encoding’ technique:



import Basis

public extension Information 

    mutating func append(
        _ string: String,
        encoding: String.Encoding = .utf8
    ) 
        guard let knowledge = string.knowledge(utilizing: encoding) else 
            return
        
        append(knowledge)
    


Subsequent, we’d like one thing that may assemble the HTTP multipart physique knowledge, for this objective we will construct a MultipartRequest object. We are able to set the boundary once we init this object and we will append the components wanted to assemble the HTTP physique knowledge.


The non-public strategies will assist to assemble all the pieces, we merely append string values to the non-public knowledge object that holds our knowledge construction. The general public API solely consists of two add features that you need to use to append a key-value primarily based type area or a whole file utilizing its knowledge. πŸ‘


public struct MultipartRequest 
    
    public let boundary: String
    
    non-public let separator: String = "rn"
    non-public var knowledge: Information

    public init(boundary: String = UUID().uuidString) 
        self.boundary = boundary
        self.knowledge = .init()
    
    
    non-public mutating func appendBoundarySeparator() 
        knowledge.append("--(boundary)(separator)")
    
    
    non-public mutating func appendSeparator() 
        knowledge.append(separator)
    

    non-public func disposition(_ key: String) -> String 
        "Content material-Disposition: form-data; identify="(key)""
    

    public mutating func add(
        key: String,
        worth: String
    ) 
        appendBoundarySeparator()
        knowledge.append(disposition(key) + separator)
        appendSeparator()
        knowledge.append(worth + separator)
    

    public mutating func add(
        key: String,
        fileName: String,
        fileMimeType: String,
        fileData: Information
    ) 
        appendBoundarySeparator()
        knowledge.append(disposition(key) + "; filename="(fileName)"" + separator)
        knowledge.append("Content material-Sort: (fileMimeType)" + separator + separator)
        knowledge.append(fileData)
        appendSeparator()
    

    public var httpContentTypeHeadeValue: String 
        "multipart/form-data; boundary=(boundary)"
    

    public var httpBody: Information 
        var bodyData = knowledge
        bodyData.append("--(boundary)--")
        return bodyData
    


The final remaining two public variables are helpers to simply get again the HTTP associated content material kind header worth utilizing the correct boundary and the entire knowledge object that it is best to to ship to the server. Here is how one can assemble the HTTP URLRequest utilizing the multipart struct.


var multipart = MultipartRequest()
for area in [
    "firstName": "John",
    "lastName": "Doe"
] 
    multipart.add(key: area.key, worth: area.worth)


multipart.add(
    key: "file",
    fileName: "pic.jpg",
    fileMimeType: "picture/png",
    fileData: "fake-image-data".knowledge(utilizing: .utf8)!
)


let url = URL(string: "https://httpbin.org/put up")!
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.setValue(multipart.httpContentTypeHeadeValue, forHTTPHeaderField: "Content material-Sort")
request.httpBody = multipart.httpBody


let (knowledge, response) = attempt await URLSession.shared.knowledge(for: request)

print((response as! HTTPURLResponse).statusCode)
print(String(knowledge: knowledge, encoding: .utf8)!)


As you’ll be able to see it is comparatively simple, you simply add the shape fields and the recordsdata that you simply wish to add, and get again the HTTP associated values utilizing the helper API. I hope this text will assist you to simulate type submissions utilizing multipart requests with out trouble. 😊




Source link

Share30Tweet19
learningcode_x1mckf

learningcode_x1mckf

Recommended For You

Swift Against Humanity – Hacking with Swift

by learningcode_x1mckf
April 1, 2023
0
Swift Against Humanity – Hacking with Swift

So that you suppose you already know Swift? Assume once more! Recent from the success of our audiobook launch, Laboratoires TwoStraws is again with an all-new card sport...

Read more

Introducing MotionBar – the Animated Tab Bar Library

by learningcode_x1mckf
March 31, 2023
0
Introducing MotionBar – the Animated Tab Bar Library

In an earlier tutorial, we shared find out how to create a customized tab bar view utilizing SwiftUI to exchange the usual tab bar. If you happen to’ve...

Read more

Introducing – AI Utils for macOS

by learningcode_x1mckf
March 24, 2023
0
Introducing – AI Utils for macOS

⚠️ REQUIRES a FREE OpenAI API key. You will get one utilizing this hyperlink. ⚠️ Improve your productiveness with our AI powered utility application. - accessible...

Read more

Encoding and decoding data using the Hummingbird framework

by learningcode_x1mckf
March 22, 2023
0
Encoding and decoding data using the Hummingbird framework

HTTP is all about sending and receiving information over the community. Initially it was solely utilized to switch HTML paperwork, however these days we use HTTP to switch...

Read more

Hummingbird routing and requests – The.Swift.Dev.

by learningcode_x1mckf
March 17, 2023
0
Hummingbird routing and requests – The.Swift.Dev.

Routing on the server facet means the server goes to ship a response primarily based on the URL path that the consumer referred to as when firing up...

Read more
Next Post
The Future of C++ | Dice.com Career Advice

The Future of C++ | Dice.com Career Advice

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Related News

Google expands open source bounties, will soon support Javascript fuzzing too – ZDNet

5 Reasons Every JavaScript Developer Hasn't Already Moved to … – Medium

February 5, 2023
Time limit for notify – JavaScript – SitePoint Forums

How to wait for the page to load before clicking in javascript? – JavaScript – SitePoint Forums

October 26, 2022
Azul on helping Filipino customers run Java apps efficiently – Manila Bulletin

Azul on helping Filipino customers run Java apps efficiently – Manila Bulletin

September 23, 2022

Browse by Category

  • C#
  • C++
  • Java
  • JavaScript
  • Python
  • Swift

RECENT POSTS

  • So why did they decide to call it Java? – InfoWorld
  • Senior Java Developer – IT-Online
  • 4 Packages for Working With Date and Time in JavaScript – MUO – MakeUseOf

CATEGORIES

  • C#
  • C++
  • Java
  • JavaScript
  • Python
  • Swift

Β© 2022 Copyright Learning Code

No Result
View All Result
  • Home
  • JavaScript
  • Java
  • Python
  • Swift
  • C++
  • C#

Β© 2022 Copyright Learning Code

Are you sure want to unlock this post?
Unlock left : 0
Are you sure want to cancel subscription?