Home > Blockchain >  ionic httpClient does wrong request?
ionic httpClient does wrong request?

Time:02-15

So I was trying to make a quick test for my API and I'm getting this weird issue...

This is my code for my ApiService:

import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Injectable } from '@angular/core';

@Injectable()
export class ApiService {

    constructor(private http: HttpClient) { }

    async getData(url, method): Promise<Object>{
        let fullurl = url   method
        console.log(fullurl);
        return await new Promise(resolve=>{
            this.http.get(fullurl)
            .subscribe(data=>{
                resolve(data);
                return data;
            },error=>{
                console.log(error);
            });
        });
    }
}

And this is how I call my service:

import { Component, OnInit } from '@angular/core';
import { ApiService } from '../services/api.service';

@Component({
  selector: 'app-home',
  templateUrl: 'home.page.html',
  styleUrls: ['home.page.scss'],
  providers:[ApiService]
})
export class HomePage implements OnInit {

  constructor( private api:ApiService,) {}
  url:string="api.test.tk/"
  method:string="SangreDragon"
  json:any
  async ngOnInit(){
    console.log(this.url  this.method);
    this.json = await this.api.getData(this.url, this.method);
    await console.log(this.json);
  } 
}

The GET call is going, somehow, to: http://localhost:8100/api.test.tk/SangreDragon

Im using the Ionic serve to test this... Should I compile to test it?

WHY?

CodePudding user response:

So apparently... if you don tput the http:// on the URL, it tries to go to localhost... So it's fixed. Thanks for reading I guess.

  • Related