Jonathan002
Jonathan002

Reputation: 10268

How can I configure a proxy with my http request?

I'm very new to http request and am trying to get data with https://darksky.net/dev/docs/faq. They don't support CORS and suggest that I have to use a proxy server to get their data.

I'm using Angular and am using a service for the http request:

import { Injectable } from '@angular/core';
import { Headers, Http } from '@angular/http';
import 'rxjs/add/operator/toPromise';

@Injectable()
export class ServiceWeather { // Unconventional but I see all my services when typing service
    constructor(private http: Http) { }

    private weatherUrl = 'https://api.darksky.net/forecast/[key]/[latitude],[longitude]'

    public getWeather(): Promise<any> {
        return this.http.get(this.weatherUrl)
                .toPromise()
                .then(response => response.json().data)
                .catch(this.handleError);
    }

    private handleError(error: any): Promise<any> {
        console.error('An error occurred', error);
        return Promise.reject(error.message || error);
    }
}

How can configure this with a proxy server?

Helpful Extra Info Needed: What proxy server services should I use for development?

Upvotes: 1

Views: 1118

Answers (1)

Jonathan002
Jonathan002

Reputation: 10268

Thanks for the comment on 'Cors Proxy'.

I had found the service: http://cors-proxy.htmldriven.com and was able to get data from darksky by using their url.

private weatherUrl = 'http://cors-proxy.htmldriven.com/?url=https://api.darksky.net/forecast/[key]/[latitude],[longitude]'

Upvotes: 1

Related Questions