0
0
iOS Swiftmobile~20 mins

POST request with JSON body in iOS Swift - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Swift POST Request Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ui_behavior
intermediate
2:00remaining
What happens when you send a POST request with JSON body in Swift?

You create a POST request in Swift with a JSON body. What is the expected behavior when the request is sent?

iOS Swift
let url = URL(string: "https://example.com/api")!
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
let json: [String: Any] = ["name": "John", "age": 30]
request.httpBody = try? JSONSerialization.data(withJSONObject: json)

let task = URLSession.shared.dataTask(with: request) { data, response, error in
  if let error = error {
    print("Error: \(error)")
  } else if let data = data {
    print("Response data received")
  }
}
task.resume()
AThe server receives a POST request with a JSON body containing the name and age data.
BThe server receives a GET request with URL parameters instead of a JSON body.
CThe request fails because JSONSerialization cannot convert the dictionary to data.
DThe request sends an empty body because httpBody is not set correctly.
Attempts:
2 left
💡 Hint

Check the httpMethod and httpBody properties of the URLRequest.

📝 Syntax
intermediate
2:00remaining
Which code snippet correctly sets the JSON body for a POST request in Swift?

Choose the code snippet that correctly sets a JSON body for a POST request using URLRequest in Swift.

Arequest.httpBody = try? JSONSerialization.data(withJSONObject: ["key": "value"])
Brequest.httpBody = JSONSerialization.jsonObject(with: ["key": "value"])
Crequest.httpBody = Data(["key": "value"])
Drequest.httpBody = "{\"key\":\"value\"}"
Attempts:
2 left
💡 Hint

Look for the method that converts a dictionary to Data.

lifecycle
advanced
2:00remaining
What happens if you forget to call resume() on the URLSession data task?

Consider this Swift code creating a POST request with JSON body but missing a call. What is the effect?

let task = URLSession.shared.dataTask(with: request) { data, response, error in
  // handle response
}
AThe request is sent automatically without calling resume().
BThe request is never sent because resume() was not called on the task.
CThe app crashes with a runtime error.
DThe request sends but the completion handler is never called.
Attempts:
2 left
💡 Hint

Check how URLSession tasks start their work.

🔧 Debug
advanced
2:00remaining
Why does this POST request with JSON body fail to send data?

Review this Swift code snippet. The POST request is sent but the server receives no JSON data. What is the likely cause?

var request = URLRequest(url: URL(string: "https://api.example.com")!)
request.httpMethod = "POST"
let json = ["user": "alice"]
request.httpBody = try? JSONSerialization.data(withJSONObject: json)
// Missing Content-Type header
let task = URLSession.shared.dataTask(with: request) { data, response, error in
  // handle response
}
task.resume()
AThe URL is invalid, so the request fails silently.
BThe httpMethod is not set to POST, so the body is ignored.
CJSONSerialization.data(withJSONObject:) throws an error and returns nil.
DThe Content-Type header is missing, so the server does not recognize the body as JSON.
Attempts:
2 left
💡 Hint

Check the HTTP headers required for JSON data.

🧠 Conceptual
expert
2:00remaining
What is the correct way to handle JSON encoding errors in a POST request in Swift?

You want to send a POST request with a JSON body. How should you handle possible errors during JSON encoding?

AUse try? to convert JSON to Data and send the request only if data is not nil.
BUse try! to force JSON encoding and crash if it fails.
CUse a do-catch block to catch encoding errors and handle them gracefully before sending.
DIgnore errors and send an empty body if encoding fails.
Attempts:
2 left
💡 Hint

Think about safe error handling in Swift.