How to make requests to connect to third party APIs in Vapor?

2.5k views Asked by At

In the Vapor framework for server side swift, I would like to respond to a request with info I got from third party API. For example, I receive a get request asking for the temperature of a city, and I want to connect to yahoo whether API to get the temperature then send it back. Do I need to download packages like Alamofire? Or Is there a built in way to do so in Vapor?

1

There are 1 answers

2
tobygriffin On BEST ANSWER

There is a built-in HTTP client in Vapor; it is called Client.

To make a GET request to your third party API:

let apiResponse = try drop.client.get("https://api.com")

You can pass your query parameters in the query string, or using the convenient dictionary method:

let apiResponse = try drop.client.get("https://api.com", query: ["q": queryString])

Client also supports POST, or any other HTTP method.