I found this tutorial about what a 502 response code means:
What is a 502 bad gateway and how do you fix it?.
You suspected that the error was related to the server being flooded with calls, and the tutorial confirms that server overload can result in the server sending a 502 response code.
So... the high-level solution would be to stop overloading the server. However you might also want to discuss the situation with the owner of the server and find out if the overloading is entirely your fault or if, perhaps, there are other users contributing to the overload. It's also possible that the server is deliberately reacting to your inputs by banning you from sending requests. Again, a discussion with the owner of the server might help to clarify that. Perhaps, for example, there's a different API where you can send a smaller number of requests to get the large quantity of data which you need.