简体   繁体   中英

Angular HttpParams object empty

i'm working with angular version 11.2.1, and i'm trying to make an http request, a Get request with parameters and headers, the thing is, when i console.log them, they are empty, both of them, and the python is backend is throwing a 500 error.

在此处输入图像描述

this is the function in the service.ts

getOrigenes(): Observable<any>{ 
    
    const header = new HttpHeaders();
    header.set("Content-Type", "application/json");
    header.set("Authorization",  "Bearer md53b2b0eef781e8a6aaf7cf0565b780845");
    
    const params = new HttpParams();
    params.set('is_enum', '0');
    params.set('model', 'tc_interface_format');
    params.set('schema', 'acl_koncilia');

    const headersArrays = header.keys().map(x => ({ [x]: header.get(x) }));
    console.log(JSON.stringify(headersArrays));


    const paramsArray = params.keys().map(x => ({ [x]: params.get(x) }));
    console.log(JSON.stringify(paramsArray));
    
    return this.http.get('http://127.0.0.1:5000/read', {headers: header, params: params});

}

I'm not an Angular or TypeScript expert, so any help or advice would be appreciated.

HttpHeaders is an immutable structure, the set method does not add a key to the parameters, it creates a clone of the parameters with a new key inside, so you need to overwrite parameters with the result of the set method:

 params = params.set('is_enum', '0');

Details https://angular.io/api/common/http/HttpHeaders#set

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM