I'm using local angular app and I get problem to get data from API on external server. I tried to use proxy, so I create file proxyconfig.json and I included it in command line via
ng serve --proxy-config proxyconfig.json
And here is content:
{
"/api/*": {
"target": "https://bittrex.com/api/v1.1/public/",
"secure": false,
"pathRewrite": {
"^/api": ""
},
"changeOrigin": true
}
}
I need to pass variables so I created service OrderBookService :
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs';
import { OrderBook } from './order-book.model';
@Injectable({
providedIn: 'root',
})
export class OrderBookService {
constructor(private httpClient: HttpClient) {
}
getOrderBookBittrex(currency1: string, currency2: string): Observable<OrderBook[]> {
const url = `http://localhost:4200/api/getorderbook?market=${currency1}-${currency2}&type=both`;
return this.httpClient.get<OrderBook[]>(url);
}
}
The problem is when I want to get this data and save it to variable in my component, path is not translating properly: it's sending request to http://localhost:4200/api/getorderbook?market=BTC-LTC&type=both instead of https://bittrex.com/api/v1.1/public/getorderbook?market=BTC-LTC&type=both :
private getTransfers(): void {
const currency1 = 'BTC';
const currency2 = 'LTC';
this.orderBookService.getOrderBookBittrex(currency1, currency2)
.subscribe(orders => {
this.orderBook = orders;
});
}
Anybody knows how to do it properly?
It's working fine when I put all path to proxconfig.json like this:
{
"/api/*": {
"target": "https://bittrex.com/api/v1.1/public/getorderbook?market=BTC-LTC&type=both",
"secure": false,
"pathRewrite": {
"^/api": ""
},
"changeOrigin": true
}
}
But I need to pass variables.
You code
getOrderBookBittrex(currency1: string, currency2: string): Observable<OrderBook[]> {
const url = `http://localhost:4200/api/getorderbook?market=${currency1}-${currency2}&type=both`;
return this.httpClient.get<OrderBook[]>(url);
}
Just change url like
getOrderBookBittrex(currency1: string, currency2: string): Observable<OrderBook[]> {
const url = `/api/getorderbook?market=${currency1}-${currency2}&type=both`;
return this.httpClient.get<OrderBook[]>(url);
}
You don't need use http://localhost:4200 in you code.
And "pathRewrite": { "^/api": "" },
don't need too
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.