Laxystem
05/22/2024, 12:37 PMLaxystem
05/22/2024, 12:40 PMLaxystem
05/22/2024, 12:42 PMBruce Hamilton
05/22/2024, 12:50 PMclient.monitor.subscribe(HttpResponseRedirectEvent) { response ->
println(response.status)
}
Laxystem
05/22/2024, 12:51 PMLaxystem
05/22/2024, 12:56 PMLaxystem
05/22/2024, 12:56 PMLaxystem
05/22/2024, 1:04 PMcall.attributes
copied to the request?Bruce Hamilton
05/22/2024, 1:15 PMLaxystem
05/22/2024, 1:16 PMresponse.call.request.attributes
should work?Bruce Hamilton
05/22/2024, 1:17 PMAleksei Tirman [JB]
05/22/2024, 7:06 PMHttpResponseRedirectEvent
is available only in Ktor 3.. To solve the problem for Ktor 2., you can write a plugin that would add a handler for the Send
hook to save the latest redirected location in the request attributes. Here is an example:
val redirectKey = AttributeKey<String>("redirectKey")
val plugin = createClientPlugin("my plugin") {
on(Send) { request ->
val call = proceed(request)
if (listOf(301, 302, 307, 308).contains(call.response.status.value)) {
call.response.headers[HttpHeaders.Location]?.let { location ->
request.attributes.put(redirectKey, location)
}
}
call
}
}
val client = HttpClient(CIO) {
install(plugin)
}
val response = client.get("<https://httpbin.org/status/302>")
println(response.request.attributes[redirectKey])
Laxystem
05/27/2024, 2:05 PMclient.monitor.subscribe(HttpRedirect.HttpResponseRedirect) {
if (it.status == HttpStatusCode.SeeOther && FirstSeeOtherRedirectLocation !in it.call.attributes) {
it.call.attributes.put(FirstSeeOtherRedirectLocation, it.request.url)
}
}
Laxystem
05/27/2024, 2:06 PMSajal Kumar Jana
05/28/2024, 8:08 AMoverride suspend fun getNoteById(noteId: Long): NoteRow? {
return dbQuery {
NotesTable
.join(
otherTable = UserTable,
onColumn = NotesTable.userId,
otherColumn = UserTable.userId,
joinType = JoinType.INNER
)
.select { NotesTable.noteId eq noteId }
.singleOrNull()
?.let { toNoteRow(it) }
}
}override suspend fun getUserNotes(userId: Long): Response<NoteResponse> {
val listOfNotes = notesDao.getUserNotes(userId)
return if (listOfNotes.isEmpty()) {
Response.Error(
code = HttpStatusCode.InternalServerError,
data = NoteResponse(
success = false,
message = "No notes available, click to add notes"
)
)
} else {
Response.Success(
data = NoteResponse(
success = true,
notes = listOfNotes.map { toNote(it) },
message = "Notes fetched successfully"
)
)
}
} route("/notes"){
delete(path = "/{noteId}") {
try {
val noteId = call.getLongParameter(name = "noteId")
val result = repository.deleteNote(noteId = noteId)
call.respond(
status = result.code,
message = result.data
)
} catch (badRequestError: BadRequestException) {
return@delete
} catch (anyError: Throwable) {
call.respond(
status = HttpStatusCode.InternalServerError,
message = NoteResponse(
success = false,
message = "An unexpected error has occurred, try again!"
)
)
}
}
get("/{userId}") {
try {
val userId = call.parameters["userId"]!!.toLong()
val response = repository.getUserNotes(userId)
call.respond(response)
} catch (badRequestError: BadRequestException) {
return@get
} catch (anyError: Throwable) {
call.respond(
status = HttpStatusCode.InternalServerError,
message = NoteResponse(
success = false,
message = "An unexpected error has occurred, try again!"
)
)
}
}
} please help me fix the issue i am getting the any error throwabele for the get response i have two tables a usertable and a notes table linked by userId i want to fetch all the notes created by a user
Aleksei Tirman [JB]
05/30/2024, 8:25 AM