[Project D] Devlog #1: Debugging a failing request

A little background

I have been working on this project for a few months now and it is one of the largest projects that I have worked on. I am currently building 2 microservices for this project and this project has introduced me to quite a number of things in spring boot that I hope to write about someday soon. With that out of the way, let's get started.

Devlog

I have been working on integrating with a third-party API for some payments that need to be handled by one of the microservices that I am working on. I will be using the service primarily to handle card payments so the idea was to use their hosted page option so that I don't have to collect card details from my frontend.

After understanding the API and how it worked, I devised the following flow

  • Take data required from the frontend (includes validation)

  • Save a copy of the payment info locally

  • Make request to third-party API

  • Use response to update payment data locally

  • Respond to frontend

Very simple, right? Right, in principle, not so much in execution. I decided to use RestTemplate to make the request to the third-party service but I immediately run into an issue. What was the issue? Well, I never got a response from the API.

The api requires that I authenticate before I make the request that I actually want to make. So my initial thought was to use an interceptor to make a request to get the jwt that I needed and then insert it into the request headers. However, because I was not getting a response back the request itself never when through. I thought this was maybe an issue with the interceptor but even after creating a method that took place of the interceptor, the same issue occurred. However, when I tested from insomnia, everything worked just fine. I later realized that the request was using a custom vendor media type instead of the standard application/json. Once I figured this out, I was able to get past the authentication; and then the real trouble begun.

After sorting out the authentication, I figured it was going to be an easy integration but this was when RestTemplate decided to fail me.

The endpoint I needed to hit to make my payment was also using a vendor specific media type different from the auth so once I discovered my mistake at that part everything was supposed to be golden, but it wasn't.

Here's what I encountered:

  • With the correct media type, the request hungs until the read timeout is hit

  • With the wrong media type it fails immediately with 415

  • Without the token/With invalid token, it fails immediately with 401

  • The request works just fine in Insomnia and the API test sandbox

  • The request still hungs after skipping the token authentication and using a hardcoded one (which I got a few seconds prior from insomnia)

I tried several things, but none of them worked. I looked at several StackOverflow posts, chatted with ChatGPT and tried various debugging options but nothing. So chatGPT recommended I switch to webflux instead. I thought to myself, I have nothing to lose, so I gave it a shot. Imagine my surprise when it worked. Turns out the data that I was sending was wrong so I fixed the data and it succeeded. I was able to make the request to the API and get the payment setup properly. So now that the data is correct, the headers are correct, it is working with webflux, you would think that the request should work with RestTemplate as well, right? Well, you would be wrong. For the life of me, I couldn't figure out why the request still failed. It connected just fine, but it always failed to read. The data that is being transmitted back to me isn't very huge so I wouldn't expect a ReadTimeout but this is currently where it's at. I forgot to mention, RestTemplate was only hanging on this particular request. The request to get the jwt for authentication uses RestTemplate and that works just fine.

Anyway, for my own sanity, I've decided to use webflux for this particular request. I would love to debug more and find out why it hungs, but there's a lot more I need to do. For instance, I need to come up with a flow that doesn't have the frontend waiting for several seconds while I do all this processing in the background in a way that makes sense.

If anyone has any ideas as to why the request might he failing, I would love to hear from you. For now, this is me signing out.

Until next time, I bid you adieu.