Hello everyone, please how can I call an endpoint ...
# getting-started
a
Hello everyone, please how can I call an endpoint in a
GET route
in
ktor
example:
<https://api.paystack.co/transaction/verify/353532>
and after I called it, how can I get the response in form of
json
a
you can use the ktor http client, which are available as separate modules from the ktor server package https://ktor.io/docs/create-client.html
a
Copy code
get("/payments/verify/paystack/{ref}") {
    val ref = call.parameters["ref"] ?: return@get call.respondText("Missing or malformed ref", status = HttpStatusCode.BadRequest)

    //verify
    val client = HttpClient(CIO)
    val response = client.get("<https://api.paystack.co/transaction/verify/${ref}>") {
        header("Authorization", "Bearer sk_test_4ty6ggfb5c94ef56c888fd576bd06841ac3f9fc364118f")

    call.respond(response)

}
above is how I’m doing it but I’m not getting the reponse
a
does that code compile? I would expect that you would need to extract values from
response
instead of forwarding it wholesale to
call.respond()
a
so, please any idea on how to extract it?
a
the
response
value is documented here: https://ktor.io/docs/response.html
a
ohk, thank you
t
are you missing a } in the snippet you shared before your call.respond call? but in general break it it up in pieces and 1. make sure your response from the ktor client part is working properly (e.g. a breakpoint on call.respond should give you that info), before thinking about 2. your ktor server part (call.respond) returning a json of what you received with your client
a
Yes, Sir @Thomas Urbanitsch I for got copy the
}
Let me share the response of the json please
This is what I’m getting
t
(in general i’m not sure what your snippet does but i would probably not share any tokens in an open slack community 😄 some “payments” api i don’t know + a hardcoded bearer token might get ppl interested)
a
Instead of
Copy code
{
  "status": "success",
  "message": "Transaction fetched successfully",
  "data": {
    "id": 1163068,
    "tx_ref": "akhlm-pstmn-blkchrge-xx6",
    "flw_ref": "FLW-M03K-02c21a8095c7e064b8b9714db834080b",
    "device_fingerprint": "N/A",
    "amount": 3000,
    "currency": "NGN",
    "charged_amount": 3000,
    "app_fee": 1000,
    "merchant_fee": 0,
    "processor_response": "Approved",
    "auth_model": "noauth",
    "ip": "pstmn",
    "narration": "Kendrick Graham",
    "status": "successful",
    "payment_type": "card",
    "created_at": "2020-03-11T19:22:07.000Z",
    "account_id": 73362,
    "amount_settled": 2000,
    "card": {
      "first_6digits": "553188",
      "last_4digits": "2950",
      "issuer": " CREDIT",
      "country": "NIGERIA NG",
      "type": "MASTERCARD",
      "token": "flw-t1nf-f9b3bf384cd30d6fca42b6df9d27bd2f-m03k",
      "expiry": "09/22"
    },
    "customer": {
      "id": 252759,
      "name": "Kendrick Graham",
      "phone_number": "0813XXXXXXX",
      "email": "<mailto:user@example.com|user@example.com>",
      "created_at": "2020-01-15T13:26:24.000Z"
    }
  }
}
t
yes, you are getting a HttpResponse because thats what your response val is, returned by the ktor client Which is mentioned in the response link @August Lilleaas shared already. Which also explains how to get the body of that HttpResponse
a
okay, thank you all
Thank you all, I read the string using
Copy code
jacksonObjectMapper().readTree(stringBody)
🥳 1