no data being returned from Notion API call

Im using Notions API to print out some data from one of my own pages in notion and im using URLSession to make the request then parsing the unwrapped data but nothing is being returned to my console and I know my endpoint and API key is correct. I've gone through the notion API documentation can't can't seem to find anything in it that I am not doing or doing wrong. Ill provide my code as well as the documentation I've been consulting: https://developers.notion.com/reference/intro


import Foundation


struct Page: Codable {
    let id: String
    let title: String

    
}

let endpoint = URL(string: "https://api.notion.com/v1/pages/8efc0ca3d9cc44fbb1f34383b794b817")
let apiKey = "… redacted …"
let session = URLSession.shared



func makeRequest() {
    if let endpoint = endpoint {
        
        
        let task = URLSession.shared.dataTask(with: endpoint) { data, response, error in
            if let taskError = error {
                print("could not establish url request:\(taskError)")
                return
            }
            
            if let unwrapData = data {      //safely unwrapping the data value using if let
                do {
                       let decoder = JSONDecoder()                                 //JSONDecoder method to decode api data,
                       let codeUnwrappedData = try decoder.decode(Page.self,from: unwrapData)     //type: specifies its a struct, from: passes the data parmeter that contains the api data to be decoded
                } catch {
                    print("could not parse json data")
                }
            }
            
            
            if let httpResponse = response as? HTTPURLResponse {
                if httpResponse.statusCode == 200 {
                    if let apiData = data {
                        print(String(data: apiData, encoding: .utf8)!)
                    }
                } else {
                    print("unsuccessful http response:\(httpResponse)")
                }
                makeRequest()
            }
      
        }
        task.resume()
    }
}

Replies

You have code to check the statusCode returned. Is that value 200?

It’s hard to debug problems like this from the client perspective because the logic that determines what to return is run on the server. See Debugging HTTP Server-Side Errors for some general hints and tips.

IMPORTANT I edited your post to redact the value for apiKey. DevForums is world readable, so it’s best not to share private stuff here. Also, it’s likely that this info has ‘leaked’ to various web indexing engines, so I recommend that you revoke that key and create a replacement.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

  • thank you ill be sure to redact info like that in my future code snippet shares going forward, ill check out the link you sent.

Add a Comment