0

I am sending a request to create event API of MS Graph. Below is my function. When I execute it, I get 'Invalid type in JSON write (MSGraphDateTimeTimeZone)' error or 'Invalid type in JSON write (MSGraphAttendee)'.

I am using their Objective-C example of the request, so not sure what I am doing wrong - https://learn.microsoft.com/en-us/graph/api/group-post-events?view=graph-rest-1.0&tabs=objc

public func reserveHotel(email: String, startDateTime: Date, endDateTime: Date, locationName: String, bodyText: String? = nil) -> Promise<Data> {
    let request = makeRequest(relativeUrl: "/me/calendar/events")
    request.httpMethod = "POST"
    request.setValue("application/json", forHTTPHeaderField: "Content-Type")
    request.setValue("outlook.timezone=\"UTC\"", forHTTPHeaderField: "Prefer")
    
    let event = MSGraphEvent()
    event.subject = "Test"
    event.isOrganizer = true
    
    event.start = MSGraphDateTimeTimeZone.fromDate(startDateTime)
    event.end = MSGraphDateTimeTimeZone.fromDate(endDateTime)
    
    if let eventText = bodyText {
        let body = MSGraphItemBody()
        body.contentType = MSGraphBodyType.html()
        body.content = eventText
        event.body = body
    }
    
    let location = MSGraphLocation()
    location.displayName = locationName
    event.location = location
    
    let attendee = MSGraphAttendee()
    let emailMS = MSGraphEmailAddress()
    emailMS.address = email
    emailMS.name = locationName
    attendee.emailAddress = emailMS
    attendee.type = MSGraphAttendeeType.required()
    event.attendees = [attendee]
    
    do {
        request.httpBody = try event.getSerializedData()
    } catch {
        log.error("Failed to create JSON request body for Microsoft Graph: \(error)")
        return Promise(error: error)
    }
    
    return processRequest(request)
}

private func processRequest(_ request: NSMutableURLRequest) -> Promise<Data> {
    return Promise { seal in
        let dataTask = MSURLSessionDataTask(request: request, client: httpClient, completion: { (data: Data?, response: URLResponse?, error: Error?) in
            // Check for server-side errors
            if let response = response as? HTTPURLResponse {
                let statusCode = response.statusCode
                if (statusCode < 200) || (statusCode > 299) {
                    let description = "Microsoft Graph service returned HTTP response status \(statusCode) for URL \(request.url?.absoluteString ?? "")"
                    seal.reject(NSError(domain: NSURLErrorDomain, code: statusCode, userInfo: [NSLocalizedDescriptionKey: description]))
                    return
                }
            }
            
            seal.resolve(data, error)
        })
        dataTask?.execute()
    }
}
tech_human
  • 6,592
  • 16
  • 65
  • 107
  • Related: https://stackoverflow.com/questions/39521430/jsonserialization-invalid-type-in-json-write-swiftvalue. The error clearly says that `MSGraphDateTimeTimeZone` cannot be serialized. – vadian Jun 24 '20 at 19:11
  • @vadian - I have looked at the post before... Solution there didn't help me. – tech_human Jun 24 '20 at 19:13
  • Read the accepted answer carefully. It describes the issue in detail. By the way the error occurs in `getSerializedData()` – vadian Jun 24 '20 at 19:15

0 Answers0