SWIFT POST products.json Returning error code 200 with nil body

Solved
Excursionist
13 1 0

As the title suggests, I am attempting to add a new product to my store via the Admin API products.json via POST.

 

I did some searching in the forums on the topic, and I checked two things. One, I do have read/write access enabled for products. Two, I "believe" I have disabled cookies in the request with httpShouldHandleCookies = false, as this seems to be the most likely cause. Here is my code:

 

class func createNewSerializedRentalProduct(rentalInstrument:AimsiRentalInstrument, serializedInstrument:AimsiSerializedInstrument, completion: @escaping (ProductViewModel) -> Void) {
        let urlString = "https://" + apiKey + ":" + apiPassword + "@mystore.myshopify.com/admin/api/2019-07/products.json"
        guard let url = URL(string: urlString) else {return}
        var request = URLRequest(url: url)
        request.httpMethod = "POST"
        let session = URLSession.shared
        request.httpShouldHandleCookies = false
        
        let parameters:[String:Any] = ["product":["title":"\(rentalInstrument.category) Rental | \(rentalInstrument.sku)", "body_html":rentalInstrument.instrumentName, "vendor":"Yandas Music", "product_type":"\(rentalInstrument.category) Rental", "variants":["option1":"Serial Number", "price":"50.00", "sku":serializedInstrument.serial]]]
        
        print(parameters)
        
        do {
            request.httpBody = try JSONSerialization.data(withJSONObject: parameters, options: .prettyPrinted)
        } catch let error {
            print(error.localizedDescription)
        }

        //HTTP Headers
        request.addValue("application/json", forHTTPHeaderField: "Content-Type")
        request.addValue("application/json", forHTTPHeaderField: "Accept")
        
        session.dataTask(with: request) { (data, response, error) in
            if error != nil {
                print(error!)
            } else {
                if let data = data {
                    let json = JSON(data)
                    print(json)
                    print(data)
                    print(response)
                    
                }
            }
        }.resume()
    }
0 Likes
Highlighted
Shopify Expert
9786 86 1531

Do you have an example of the data you're sending to the API? That's a good place to start as we only see aspects of your code and the not the result of it.

If you won't want to share - that's aok. Take the response and pop it into Postman (or similar) instead. If that call then works you can be quite confident it's down to the set up of your stack. 

 

If you're really, really stuck then grab the Request ID from the headers in the response and paste it here.

★ Winning Partner of the Build a Business competition. ★ http://freakdesign.com.au
0 Likes
Excursionist
13 1 0

I put the request into postman, which revealed I did incorrectly format my JSON. After changing the formatting in postman the POST request goes through normally.

 

I made the correction in my SWIFT project, but I'm still getting the same error. It's sending me to the login screen, as if I'm not logged in? Here is the JSON in the body of the request:

{

  "product" : {

    "title" : "Clarinet Rental | USEDARTLEYCLART",

    "body_html" : "Used Artley Clarinet",

    "variants" : [

      {

        "sku" : "7000813",

        "price" : "50.00",

        "option1" : "Serial Number"

      }

    ],

    "vendor" : "Yandas Music",

    "product_type" : "Clarinet Rental"

  }

}

0 Likes
Excursionist
13 1 0

The only difference between the two request bodies is that SWIFT likes to put in a few spaces, as you can see above. Would this possibly cause the error I'm experiencing?

0 Likes

Success.

Excursionist
13 1 0

I removed the extra spaces from my [String:Any] before it was cast as json. Apparently this does the trick. The Admin API does not want ANY extra spaces. Even if XCode automatically puts them in.

0 Likes