angular / in-memory-web-api

The code for this project has moved to the angular/angular repo. This repo is now archived.
MIT License
1.18k stars 232 forks source link

Can't access request headers in GET/POST overrride #156

Open randallfox opened 6 years ago

randallfox commented 6 years ago

Can't access request headers in override. The typed RequestInfo doesn't have them. I did find them when I cast the requestinfo variable/parameter as an 'any'.. So there is a work around. Would rather use the variable as it's typed implementation though.. Here is the code:

//Note:  I had to retype this, so there might be minor syntax issues..

post( reqInfo: RequestInfo) :Observable<any> {
    // the following will cause an error (no "headers" type)
            let xxx = reqInfo.req.headers.get("X-HTTP-MyHeader");

    // but the following works:
            let rq:any = reqInfo as any;
            if ( rq.req.headers.has("X-HTTP-MyHeader")) {
                       let customHeaderValue = rq.req.headers.get("X-HTTP-MyHeader");
             // this is also usable: rq.req.headers.forEach( y => console.log(rq.req.headers.get(y)))

I guess my biggest concern is if this work around is safe to use or will it be wiped in a future release? I looked at the demo code in github for the sample override, and I think I'm doing it right. If I'm not, please let me know what IMPORT command I need.. thx

wardbell commented 6 years ago

This seems like a bug to me. I'm sorry I haven't been back here to see your report in so long.

I cannot understand why the headers on RequestInfo are other than the headers from the original req, supplemented with the 'Content-Type': 'application/json' header. I believe that was the original intention.

As you discovered, the original headers ARE preserved in RequestInfo.req.headers. You can use those.

I hope to fix it soon.

And I will make sure the fix preserves the original headers in RequestInfo.req.headers just as it does today. So you won't be broken.