How to GET API with curl in Xcode (Find URL)

Viewed 355

I am new to programming in general and was really hoping to access the ProPublica API. I'm not quite sure how to call the data into Xcode.

Its HTTP Request is:

GET https://api.propublica.org/congress/v1/bills/upcoming/{chamber}.json

The example call is:

curl "https://api.propublica.org/congress/v1/bills/upcoming/house.json" 

-H "X-API-Key: PROPUBLICA_API_KEY"

I've already gotten the key and it is valid.

Additionally, I've done work with simple url requests like:

api.openweathermap.org/data/2.5/weather?q={city name}&appid={API key}

When I used the open weather api, I could type in the link into my browser and get a huge json read out. I've been able to just parse the Json through the openweather link but this format looks a lot different to me.

Any help is appreciated! Thanks!

Edit: After lots of research it seems to be using get requests in rest api. I'm still a little confused after watching several videos and would really appreciate an example.

1 Answers

After more research I figured out how to use the GET request.

Here is the sample code I have.

    // Create URL
let urlSenateString = "https://api.propublica.org/congress/v1/bills/upcoming/house.json"


// Calls API and sets the header.
func fetchSenateUpcoming() {
    let urlHouse = URL(string: urlSenateString)!
    var request = URLRequest(url: urlHouse)
    request.setValue("application/json", forHTTPHeaderField: "Content-Type")
        request.setValue(Constant.APIKEY, forHTTPHeaderField: "X-API-Key")
}

Make sure to put the request in a function.

Related