我正在尝试为get请求之一设置标头。以下是功能:
getLeads(jwtToken: string): Observable<any>{
const headers = new HttpHeaders();
const authroizationToken = 'bearer '.concat(jwtToken);
console.log(authroizationToken); ------------------> prints the token
headers.append('Authorization', authroizationToken);
console.log(headers.get('Authorization')); ---------------------> this prints null
var result = this.http.get<Leads>(this.getLeadsUrl, {headers});
return result;
}
但是heades.get('Authorization')出于某些原因为空,我无法弄清原因。任何帮助将不胜感激。
答案 0 :(得分:1)
实际上.append
返回新的标头对象。试试这个,它按预期工作。只需确保每次添加新的标头后都将标头分配回该变量即可。
getLeads(jwtToken: string) {
let headers = new HttpHeaders();
const authroizationToken = 'bearer '.concat(jwtToken);
console.log(authroizationToken);
headers = headers.append('Authorization', authroizationToken);
console.log(headers.get('Authorization'));
}
这是工作中的Stackblitz
希望这会有所帮助:)
答案 1 :(得分:0)
对代码进行以下更改:
SELECT
SUM(
CASE WHEN ISNULL(p.promo_code)
AND h.account_number = c2.account_number THEN 1 ELSE 0 END
) as 'no_match_acct_match',
SUM(
CASE WHEN ISNULL(p.promo_code)
AND (
h.account_number <> c2.account_number
OR ISNULL(c2.account_number)
) THEN 1 ELSE 0 END
) as 'no_match',
SUM(
CASE WHEN p.promo_code IS NOT NULL
AND c.full_name = CONCAT(h.firstname, ' ', h.lastname)
AND p.start_date > @today THEN 1 ELSE 0 END
) as 'future_name',
SUM(
CASE WHEN p.promo_code IS NOT NULL
AND c.full_name = CONCAT(h.firstname, ' ', h.lastname)
AND p.start_date <= @today
AND p.`status` = 'REDEEMED' THEN 1 ELSE 0 END
) as 'redeemed_name',
SUM(
CASE WHEN p.promo_code IS NOT NULL
AND c.full_name = CONCAT(h.firstname, ' ', h.lastname)
AND p.start_date <= @today
AND p.`status` <> 'REDEEMED'
AND p.end_date < @today
AND p.end_date + 259200 >= @today THEN 1 ELSE 0 END
) as 'buffered_name',
SUM(
CASE WHEN p.promo_code IS NOT NULL
AND c.full_name = CONCAT(h.firstname, ' ', h.lastname)
AND p.start_date <= @today
AND p.`status` <> 'REDEEMED'
AND p.end_date < @today
AND p.end_date + 259200 < @today THEN 1 ELSE 0 END
) as 'expired_name',
SUM(
CASE WHEN p.promo_code IS NOT NULL
AND c.full_name = CONCAT(h.firstname, ' ', h.lastname)
AND p.start_date <= @today
AND p.`status` <> 'REDEEMED'
AND p.end_date >= @today THEN 1 ELSE 0 END
) as 'current_name',
SUM(
CASE WHEN p.promo_code IS NOT NULL
AND c.full_name <> CONCAT(h.firstname, ' ', h.lastname)
AND p.start_date > @today
AND (
r.resolve_status = 0
OR ISNULL(r.resolve_status)
) THEN 1 ELSE 0 END
) as 'future_noname',
SUM(
CASE WHEN p.promo_code IS NOT NULL
AND c.full_name <> CONCAT(h.firstname, ' ', h.lastname)
AND p.start_date <= @today
AND p.`status` = 'REDEEMED' THEN 1 ELSE 0 END
) as 'redeemed_noname',
SUM(
CASE WHEN p.promo_code IS NOT NULL
AND c.full_name <> CONCAT(h.firstname, ' ', h.lastname)
AND p.start_date <= @today
AND p.`status` <> 'REDEEMED'
AND p.end_date < @today
AND p.end_date + 259200 >= @today THEN 1 ELSE 0 END
) as 'buffered_noname',
SUM(
CASE WHEN p.promo_code IS NOT NULL
AND c.full_name <> CONCAT(h.firstname, ' ', h.lastname)
AND p.start_date <= @today
AND p.`status` <> 'REDEEMED'
AND p.end_date < @today
AND p.end_date + 259200 < @today THEN 1 ELSE 0 END
) as 'expired_noname',
SUM(
CASE WHEN p.promo_code IS NOT NULL
AND c.full_name <> CONCAT(h.firstname, ' ', h.lastname)
AND p.start_date <= @today
AND p.`status` <> 'REDEEMED'
AND p.end_date >= @today THEN 1 ELSE 0 END
) as 'current_noname'
FROM
helpdesk h FORCE INDEX (help_status)
LEFT JOIN promo_codes p ON h.promo_code = p.promo_code
LEFT JOIN customer_promocode cp ON p.promo_code = cp.promo_code
LEFT JOIN customers c ON cp.customer_number = c.customer_number
LEFT JOIN customers c2 ON h.account_number = c2.account_number
LEFT OUTER JOIN redeem_queue r ON p.promo_code = r.promo_code
WHERE
h.help_status = 0
AND h.in_view_by = ''
AND (
p.start_date >= UNIX_TIMESTAMP(SUBDATE(CURDATE(), INTERVAL 1 YEAR))
OR p.start_date IS NULL
)
AND (
ISNULL(p.promo_code)
OR ISNULL(r.promo_code)
)
group by
p.promo_code,
submitid
答案 2 :(得分:0)
您可以使用HttpHeaders的set()
方法:
在原始实例的克隆中设置或修改给定标头的值。如果标题已经存在,则将其值替换为返回对象中的给定值。
一个例子:
let header = new HttpHeaders().set(
"Authorization",`bearer ${jwtToken}`
);
return this.http.get<Leads>(this.getLeadsUrl, {headers:header});
答案 3 :(得分:0)
问题在以下主题中有答案。
https://stackoverflow.com/a/45286959/12376898
https://stackoverflow.com/a/47805759/12376898
总结起来,标头是不可变的,因此进行任何更改都会创建一个新对象。除非您将新创建的对象引用到旧引用,否则只有旧对象会保留。
getLeads(jwtToken: string) {
let headers = new HttpHeaders();
headers.append('ANYHEADER','SOMESTRING') // will be lost
headers = headers.append('ANYHEADER','SOMESTRING') //unless you reference it again to the old reference
}