I need to pass the following json to this function so Shopify Api can understand the submission.
Every time I execute this code, I get an error message that there is a missing required parameter. Obviously, I am unable to create the correct variable format and pass it to server.
Shopify API is expecting the following json to be passed via POST
{
"customer": {
"first_name": "Steve",
"last_name": "Lastnameson",
"email": "[email protected]",
"verified_email": true,
"addresses": [
{
"address1": "123 Oak St",
"city": "Ottawa",
"province": "ON",
"phone": "555-1212",
"zip": "123 ABC",
"last_name": "Lastnameson",
"first_name": "Mother",
"country": "CA"
}
]
}
}
Here is my posting code:
let customer = [
"customer": [
"first_name": "Steve",
"last_name": "Lastnameson",
"email": "[email protected]",
"verified_email": "true",
"addresses": [
[
"address1": "123 Oak St",
"city": "Ottawa",
"province": "ON",
"phone": "555-1212",
"zip": "123 ABC",
"last_name": "Lastnameson",
"first_name": "Mother",
"country": "CA",
],
],
],
] as [String: Any]
var request = URLRequest(url: URL(string: shopUrl + "/admin/customers.json")!)
request.httpMethod = "POST"
request.httpBody = try! JSONSerialization.data(withJSONObject: customer, options: [])
URLSession.shared.dataTask(with:request, completionHandler: {(data, response, error) in
if error != nil {
print(error)
} else {
do {
guard let json = try? JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? [String: Any] else { return }
guard let errors = json?["errors"] as? [[String: Any]] else { return }
if errors.count > 0 {
// show error
return
} else {
// show confirmation
}
}
}
}).resume()
The request needs to have the content type declared. Add:
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.addValue("application/json", forHTTPHeaderField: "Accept")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With