0

I am trying to build a basic rest api that returns json in flask(python) and trying to host it on heroku so that I can access the api from angular httpClient. Here's the heroku api

https://testmyak.herokuapp.com/projects

The problem is that on trying to access the api using httpClient in angular, a blank page is displayed.

The angular code has no errors whatsoever because on accessing any other fake api from json-placeholder website and it populated the page perfectly.

./app.py

from flask import Flask,jsonify
app=Flask(__name__)

@app.route('/projects')
def projects():
    x=[]
    x.append({"name":"Debal",
              "gender":"male"})
    x.append({"name":"Debdut",
            "gender":"female"})
    return jsonify(x)

if __name__=='__main__':
    app.run()

./Procfile

web: gunicorn app:app

./requirements.txt

click==7.1.2
Flask==1.1.2
gunicorn==20.0.4
itsdangerous==1.1.0
Jinja2==2.11.3
MarkupSafe==1.1.1
Werkzeug==1.0.1

And here's the angular side code ./src/app/customer.service.ts


import { Injectable } from '@angular/core';
import { HttpClient } from
'@angular/common/http';
import { ICustomer } from './customer';
import { Observable } from 'rxjs';

@Injectable()

export class CustomerService {
    _url:string='https://testmyak.herokuapp.com/projects';

    constructor(private ht:HttpClient) { }
    getCustomers():Observable<ICustomer[]>{
    return this.ht.get<ICustomer[]>(this._url);
  }
}

.src/app/customer.ts

export interface ICustomer{
    gender:string,
    name:string
}

.src/app/app.component.ts

import { Component } from '@angular/core';
import { CustomerService } from
'./customer.service';

@Component({
    selector: 'app-root',
    templateUrl: './app.component.html',
    styleUrls: ['./app.component.css']
})
export class AppComponent {
    title = 'my-app';
    myL:any[];
    constructor(private _s:CustomerService){}
    ngOnInit(){
     this._s.getCustomers().subscribe(data=>this.myL=data);

    }
}

./src/app/app.module.ts

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';

import { AppComponent } from './app.component';
import { CustomerService } from
'./customer.service'
import { HttpClientModule } from
'@angular/common/http';

@NgModule({
    declarations: [
      AppComponent
    ],
    imports: [
      BrowserModule,
      HttpClientModule
    ],
    providers: [CustomerService],
    bootstrap: [AppComponent]
})
export class AppModule { }

Please suggest the changes I should make in my code to make the api accessible using httpClient in angular

  • where is your angular side code ? – Developer Mar 26 '21 at 06:11
  • import { Injectable } from '@angular/core'; import { HttpClient } from '@angular/common/http'; import { ICustomer } from './customer'; import { Observable } from 'rxjs'; @Injectable() export class CustomerService { _url:string='https://jsonplaceholder.typicode.com/commen> constructor(private ht:HttpClient) { } getCustomers():Observable{ return this.ht.get(this._url); } } //I was using the url to check if other fake apis were working and they were – Debal Ghosh Mar 26 '21 at 06:14
  • add angular code to question too.. – Developer Mar 26 '21 at 06:16
  • This is the customer.service.ts file – Debal Ghosh Mar 26 '21 at 06:16
  • Ok am adding angular code – Debal Ghosh Mar 26 '21 at 06:17
  • You need to enable `CORS policy` to server side api, angular httpClient when request your server, serve api blocks this request because, origin is different from api server. you need to enable cors at your api side. – Developer Mar 26 '21 at 06:29
  • could you recommend how I can enable CORS policy in python flask and host it on heroku ? – Debal Ghosh Mar 26 '21 at 06:32
  • and define router method for `/project` in api like `@app.route("/projects", methods=["GET"])` – Developer Mar 26 '21 at 06:33
  • To enable CORS... before `return jsonify(x)` this statement inside api, do like this.. `response = jsonify(x); response.headers.add("Access-Control-Allow-Origin", "*"); return response;` – Developer Mar 26 '21 at 06:36
  • or you can install following package... `pip install flask-cors`. after installing it, add this to api def method `@cross_origin() def projects():` – Developer Mar 26 '21 at 06:39
  • Omygod thank you so so so so much the code works. THANK YOU YOU'RE THE BEST. I'm new to stackoverflow can I give you awards or something? – Debal Ghosh Mar 26 '21 at 06:57

0 Answers0