简体   繁体   中英

Get text from private stash link

I have application where I getting code from stash raw file. Scrapping from public repositories is simple, it looks like this:

  public getRawFile(rawLink: string) {
    return this.http.get(rawLink).map((res: Response) => res.text());
  }

But now I would like to get code from stash raw file, but from private repository. If user have access(is logged into stash) than source code from raw file is loaded.

If I trying same way, I getting respone:

    XMLHttpRequest cannot load 'private_stash_file_link'. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost' is therefore not allowed access.
    EXCEPTION: Response with status: 0  for URL: null
    Uncaught Response with status: 0  for URL: null

How can I handle this, cookies, specific options for get request, is it even possible?

EDIT 1.

Tried:

  public getRawFile(link: string) {
    let headers = new Headers();
    headers.append('Access-Control-Allow-Headers', 'Content-Type');
    headers.append('Access-Control-Allow-Methods', 'GET, OPTIONS');
    headers.append('Access-Control-Allow-Origin', '*');
    let options = new RequestOptions({headers: headers, withCredentials: true});
    return this.http.get(link, options).map((res: Response) => res.text());
  }

but same result for private repository..

plunker

The server that you are making the request to has to implement CORS to grant JavaScript from your website access (Cross Origin Resource Sharing (CORS)). So if you have access to the place where you are scraping, then add the following HTTP headers at the response of the receiving end:

Access-Control-Allow-Headers: Content-Type
Access-Control-Allow-Methods: GET, POST, OPTIONS
Access-Control-Allow-Origin: *.example.com

Make sure to replace "*.example.com" with the domain name of the host that is sending the data/getting the data. Doing this should fix your problem.

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