Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

vapor

swift3

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?

like image 749
naif Avatar asked Jan 04 '17 09:01

naif


People also ask

How do I call a third party API?

You can call third-party REST APIs directly by using a filter having Set Fields action with REST API as a data source. This filter enables you to define a one-time configuration for the third-party REST API that you want to call in Remedy workflows. You can use different HTTP methods such as GET, PUT, POST, or DELETE.

What is Vapour API?

Vapor is an open source web framework written in Swift. It can be used to create RESTful APIs, web apps, and real-time applications using WebSockets. In addition to the core framework, Vapor provides an ORM, a templating language, and packages to facilitate user authentication and authorization. Vapor. Developer(s)

What is Vapor server?

The Vapor Backend. Vapor is a framework for writing HTTP server applications, backends, and APIs in Swift. It was first created in 2016 and is an open-source project driven by the community. It allows you to create complex, full-featured applications that can power mobile applications or stand on their own.


1 Answers

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.

like image 193
tobygriffin Avatar answered Oct 12 '22 21:10

tobygriffin