0

I am trying to access SonarQube API through Fetch but it doesn't seem to be responding with any results. It just resolves with an opaque token.

Let me share what I have done so far:

fetch("http://localhost:9000/api/issues/search?pageSize=500&componentKeys=bactch_analysis_key",{
            mode:'no-cors'
        }) // Call the fetch function passing the url of the API as a parameter
            .then(
                res => res.json()
            )
            .then(data => {
                console.log(data);
            })
            .catch(err =>  {
                console.log(err);
            });

I am setting the mode as no-cors as it was throwing ACAO errors.

The same URL works perfect when openend from chrome or inspected through Postman.

This is what I get when the promise resolves: enter image description here

Below is the error that I get from the catch clause:

SyntaxError: Unexpected end of input
    at fetch.then.res (index.html:51)
VLAZ
  • 26,331
  • 9
  • 49
  • 67
Aakash Thakur
  • 3,837
  • 10
  • 33
  • 64
  • 1
    You don't seem to be providing any authorization in your request? Have you tried adding an `Authorization` header? Chrome and Postman are probably re-using your existing session, which is why they're working. – Evan Knowles May 29 '18 at 06:33
  • It will follow this format : `headers:{ 'Authorization':'Bearer '+ bearer_token }`. But what will be the `bearer_token` in this case? – Aakash Thakur May 29 '18 at 06:38
  • You can generate the `bearer_token` - see this reference: https://docs.sonarqube.org/display/SONAR/User+Token – Evan Knowles May 29 '18 at 06:40
  • 2
    Possible duplicate of [Handle response - SyntaxError: Unexpected end of input](https://stackoverflow.com/questions/43317967/handle-response-syntaxerror-unexpected-end-of-input) – sideshowbarker May 29 '18 at 06:43
  • *“I am setting the `mode` as `no-cors` as it was throwing ACAO errors”*. That’s not going to fix things. See https://stackoverflow.com/questions/43317967/handle-response-syntaxerror-unexpected-end-of-input/43319482#43319482 and https://stackoverflow.com/questions/43262121/trying-to-use-fetch-and-pass-in-mode-no-cors/43268098#43268098. You need to instead CORS-enable that `http://localhost:9000/api/issues/search` API endpoint. – sideshowbarker May 29 '18 at 06:45
  • @sideshowbarker I guess that can be done on the `.htaccess` file inside the sonarqube directory that I have AFAIK. – Aakash Thakur May 29 '18 at 06:51
  • @EvanKnowles I tried with authorization but still got the opque token. I guess the problem is of setting mode as `no-cors` only as stated by side above. – Aakash Thakur May 29 '18 at 06:52
  • @sideshowbarker I followed the method as shown in this link:https://stackoverflow.com/questions/43262121/trying-to-use-fetch-and-pass-in-mode-no-cors/43268098. It returns `https://tranquil-inlet-49324.herokuapp.com/http://localhost:9000/api/issues/search?pageSize=500&componentKeys=bactch_analysis_28_1_key 404 (Not Found)` – Aakash Thakur May 29 '18 at 08:14

0 Answers0