Ask Your Question
3

How can I make a POST request to Azure Storage REST API using Swift 5?

asked 2021-07-31 11:00:00 +0000

lalupa gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2022-06-24 15:00:00 +0000

qstack gravatar image

Here is an example of how to make a POST request to Azure Storage REST API using Swift 5:

let urlString = "<YOUR_STORAGE_URL>"
let url = URL(string: urlString)!
let request = NSMutableURLRequest(url: url)
request.httpMethod = "POST"
request.setValue("application/json", forHTTPHeaderField: "Content-Type")

// Set the body of the request
let body = // Your JSON body
request.httpBody = try? JSONSerialization.data(withJSONObject: body, options: [])

// Set the Authorization header with a shared access signature (SAS) token
let sas = "<YOUR_SAS_TOKEN>"
request.setValue(sas, forHTTPHeaderField: "Authorization")

let task = URLSession.shared.dataTask(with: request as URLRequest) { data, response, error in
    if let error = error {
        print("Error: \(error)")
        return
    }

    if let data = data {
        // Process the response data
    }
}

task.resume()

Note that you will need to replace <YOUR_STORAGE_URL> with the URL for your Azure Storage account and <YOUR_SAS_TOKEN> with a shared access signature token that provides access to the resource you want to operate on (e.g., a container, blob, or queue).

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2021-07-31 11:00:00 +0000

Seen: 12 times

Last updated: Jun 24 '22