RobertDresler / SwiftSSEStream

MIT License
6 stars 0 forks source link

Does it work with POST requests? #2

Open houmie opened 7 months ago

houmie commented 7 months ago

Hello,

Thanks for this project. Sadly it doesn't seem to work with POST requests:

Check out this line below: let decoder = JSONDecoder() This line never breaks in debugger.

       guard let urlFull = URL(string: Constants.apiServiceUrlFull) else {
            print("Invalid Full URL")
            return
        }
        var request = URLRequest(url: urlFull)
        request.httpMethod = "POST"
        request.addValue("application/json", forHTTPHeaderField: "Content-Type")

        let requestBody: [String: Any] = [
            "max_new_tokens": 256,
            "max_tokens": 256,
            "stream": true,
        ]

        do {
            let jsonData = try JSONSerialization.data(withJSONObject: requestBody, options: [])
            request.httpBody = jsonData
        } catch {
            print("Error serializing JSON: \(error)")
            return
        }

        do {
            for try await event in SSEStream(request: request) {
                let decoder = JSONDecoder()
                let joined = try event.dataArray.map { dataString in
                    let data = try decoder.decode(CustomData.self, from: Data(dataString.utf8))
                    return data.chunk
                }.joined(separator: "\n")
                print(joined) 
            }
            print("Completed")
        } catch {
            print("Completed with error:", error)
        }

Thanks

RobertDresler commented 5 months ago

@houmie hello, what error do you get on line print("Completed with error:", error)?