如何在Angular 6的请求选项中添加标题?

时间:2018-10-22 08:20:47

标签: angular observable

我正在尝试添加requestoption和标头,但它引发以下错误

library(rvest)
library(tidyverse)

pg<-read_html("https://bidplus.gem.gov.in/bidresultlists?bidresultlists&page_no=1")

##Find total number of pages

page_num<-pg%>%
  html_nodes(".pagination")%>%
  html_nodes("li")%>%
  html_nodes("a")%>%
  .[5]%>%
  html_attrs()%>%
  unlist()%>%
  parse_number()%>%unique()

 #make function for scraping page
scr=function(i){
  pg<-read_html(paste0("https://bidplus.gem.gov.in/bidresultlists?bidresultlists&page_no=",i))
  blocks <- html_nodes(pg, ".block")

  items_and_quantity <- html_nodes(blocks, xpath=".//div[@class='col-block' and contains(., 'Item(s)')]")

  items <- html_nodes(items_and_quantity, xpath=".//strong[contains(., 'Item(s)')]/following-sibling::span") %>% html_text(trim=TRUE)
  quantity <- html_nodes(items_and_quantity, xpath=".//strong[contains(., 'Quantity')]/following-sibling::span") %>% html_text(trim=TRUE) %>% as.numeric()

  department_name_and_address <- html_nodes(blocks, xpath=".//div[@class='col-block' and contains(., 'Department Name And Address')]") %>% 
    html_text(trim=TRUE) %>% 
    gsub("\n", "|", .) %>% 
    gsub("[[:space:]]*\\||\\|[[:space:]]*", "|", .)

  block_header <- html_nodes(blocks, "div.block_header")

  html_nodes(block_header, xpath=".//p[contains(@class, 'bid_no')]") %>%
    html_text(trim=TRUE) %>% 
    gsub("^.*: ", "", .) -> bid_no

  html_nodes(block_header, xpath=".//p/b[contains(., 'Status')]/following-sibling::span") %>% 
    html_text(trim=TRUE) -> status

  html_nodes(blocks, xpath=".//strong[contains(., 'Start Date')]/following-sibling::span") %>%
    html_text(trim=TRUE) -> start_date

  html_nodes(blocks, xpath=".//strong[contains(., 'End Date')]/following-sibling::span") %>%
    html_text(trim=TRUE) -> end_date

  data.frame(
    bid_no,
    status,
    start_date,
    end_date,
    items,
    quantity,
    department_name_and_address,
    stringsAsFactors=FALSE
  ) -> xdf
  xdf$is_ra <- grepl("/RA/", bid_no)
  return(xdf)
}
#run for-loop for each page and save it in data frame  
res<-1:page_num%>%
  map_df(.,scr)

#for example 
1:2%>%
   map_df(.,scr)%>%
   head(5)
            bid_no               status          start_date            end_date                                                   items quantity
1 GEM/2018/B/94492        Not Evaluated 02-10-2018 10:42:am 22-10-2018 01:00:pm door frame metal detector dfmd  security metal detector        1
2 GEM/2018/B/95678        Not Evaluated 29-09-2018 11:01:am 22-10-2018 01:00:pm                                         Foolscap sheets      100
3 GEM/2018/B/96187        Not Evaluated 01-10-2018 10:29:am 22-10-2018 01:00:pm                               OEM Cartridge/ Consumable       20
4 GEM/2018/B/96196        Not Evaluated 01-10-2018 10:48:am 22-10-2018 01:00:pm                               OEM Cartridge/ Consumable       20
5 GEM/2018/B/96722 Technical Evaluation 01-10-2018 05:26:pm 22-10-2018 01:00:pm        Special Purpose Telephones(smart phone for ICDS)    33914
                                                                                          department_name_and_address is_ra
1 Department Name And Address:||Ministry Of Shipping Na Electronics Directorate General Of Lighthouses And Lightships FALSE
2                            Department Name And Address:||Ministry Of Defence Department Of Defence Cweafborjhar N/a FALSE
3                            Department Name And Address:||Ministry Of Defence Department Of Defence Cweafborjhar N/a FALSE
4                            Department Name And Address:||Ministry Of Defence Department Of Defence Cweafborjhar N/a FALSE
5                                 Department Name And Address:||Bihar Social Welfare Department Bihar Procurement N/a FALSE

以下是我的代码

    src/app/pages/ldapp/ldapp.service.ts(72,5): error TS2322: Type 'HttpHeaders' is not assignable to type 'Headers'.
    Property 'forEach' is missing in type 'HttpHeaders'.
    src/app/pages/ldapp/ldapp.service.ts(74,31): error TS2345: Argument of type 'RequestOptions' is not assignable to parameter of type '{ headers?: HttpHeaders | { [header: string]: string | string[]; }; observe?: "body"; params?: Ht...'.
    Types of property 'headers' are incompatible.
        Type 'Headers' is not assignable to type 'HttpHeaders | { [header: string]: string | string[]; }'.
        Type 'Headers' is not assignable to type '{ [header: string]: string | string[]; }'.
            Index signature is missing in type 'Headers'

1 个答案:

答案 0 :(得分:0)

HttpHeaders.append(...)使用添加的新标头创建一个新的HttpHeaders对象,它不会更改您当前的HttpHeaders对象实例。

这应该可以解决您的问题:

this.headers = this.headers.append('Authorization', 'Bearer ' + this.appState.get('_a_token'));

请记住,标头是不可变的,这意味着对标头所做的任何修改都会返回一个新实例,它不会使对象本身发生变异。