Bird
0
0

Which Swift code snippet correctly sets the HTTP method to POST and adds a JSON Content-Type header?

easy📝 Syntax Q12 of 15
iOS Swift - Networking
Which Swift code snippet correctly sets the HTTP method to POST and adds a JSON Content-Type header?
Arequest.httpMethod = "POST" request.setValue("application/json", forHTTPHeaderField: "Content-Type")
Brequest.method = "POST" request.addHeader("Content-Type", value: "application/json")
Crequest.httpMethod = POST request.setHeader("Content-Type", "application/json")
Drequest.method = POST request.setValue("json", forHTTPHeaderField: "Content-Type")
Step-by-Step Solution
Solution:
  1. Step 1: Set HTTP method correctly

    The property to set HTTP method in URLRequest is httpMethod and it expects a String like "POST".
  2. Step 2: Add JSON Content-Type header properly

    Use setValue(_:forHTTPHeaderField:) with "application/json" for the "Content-Type" header.
  3. Final Answer:

    request.httpMethod = "POST"\nrequest.setValue("application/json", forHTTPHeaderField: "Content-Type") -> Option A
  4. Quick Check:

    Correct method and header syntax = A [OK]
Quick Trick: Use request.httpMethod and setValue for headers [OK]
Common Mistakes:
  • Using wrong property names like method or setHeader
  • Forgetting quotes around "POST"
  • Using incorrect header field names or values

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More iOS Swift Quizzes