ios – Ship knowledge to Airtables by a POST request with a json physique

[ad_1]

Im attempting to ship knowledge to Airtables utilizing its REST APIs primarily based on the documentation. The half Im caught is including the parameters. It throws me an error saying “Invalid request: parameter validation failed. Verify your request knowledge“.

The Curl based on the doc as bellow.

enter image description here

My partially achieved code as bellow.

    let table_name = "Dietpercent20Plan"
    let base_id = "appmrzybooFj9mFVF"
    let token = "SomeID"
      
      // put together json knowledge
    let json: [String: String] = ["Food Type": "Minseee",
                                     "Person Name": "Rabiit Con"]
    
    // create the url with URL
    let url = URL(string: "https://api.airtable.com/v0/(base_id)/(table_name)")! // change server url accordingly

    let jsonData = attempt? JSONSerialization.knowledge(withJSONObject: json)

    var request = URLRequest(url: url)
    request.httpMethod = "POST"
    request.setValue("software/json", forHTTPHeaderField: "Content material-Sort")
    request.addValue("software/json", forHTTPHeaderField: "Settle for")
    request.setValue( "Bearer (token)", forHTTPHeaderField: "Authorization")
    
    request.httpBody = jsonData

    let process = URLSession.shared.dataTask(with: request) { knowledge, response, error in
        guard let knowledge = knowledge, error == nil else {
            print(error?.localizedDescription ?? "No knowledge")
            return
        }
        let responseJSON = attempt? JSONSerialization.jsonObject(with: knowledge, choices: [])
        if let responseJSON = responseJSON as? [String: Any] {
            print(responseJSON) //Code after Successfull POST Request
        }
    }

    process.resume()
}

[ad_2]

Leave a Reply

Your email address will not be published. Required fields are marked *