1

I have a select-dropdown-list, it's data is generated from a API/json file. The json resembles the following:

[
    {
        "id": 444,
        "ggg_progid": "0000-111-222-333-44444",
        "clientName": "client 1- aaa-bb-01-05 - AA"
    }
]

...but I get the error, Unhandled Rejection (SyntaxError): Unexpected end of JSON input, when I run my code using a particular url for the API/json file. So when I copy the code from the API and paste it into myjson.com, then use the url provided by myjson.com, my code works fine.

What could the problem be, both API's have the same json syntax?

Just in case, I provided the code below:

import React, { Component } from 'react';

class Ast extends Component {

   constructor(){
       super();
       this.state = {
           data: [],
       };
   } //end constructor

    componentDidMount() {
        fetch('http://myapi/:1234/api/clients', {
            method: 'GET',
            headers: {
                'Content-type': 'application/json',
                'Authorization': 'Bearer abc123def.abc123def.abc123def'
            },
        })
        .then(results => results.json())
        .then(data => this.setState({ data: data }))
    }

    render() {
    console.log(this.state.data);
    return (
    <div>
        <div className="container">
            <form>
            <h2>Memeber Selection:</h2>
            <div>
                {
                    this.state.data.map(item =>(
                        <div>
                            <select className="custom-select" id="clientName">>
                            <option key={item.clientName}>{item.clientName}</option>
                            </select>
                        </div>
                    ))
                }
            </div>
            <div>
            <button type="submit" className="btn btn-primary">Submit</button>
            </div>
            </form>
        </div> 
    </div>
        );
      }
}

export default Ast

I took the failing URL and pasted it into postman, and it renders results. Any ideal why an error is generated when I use the url in my code?

3
  • Have you tried console.log()-ing the data? Commented May 11, 2018 at 15:56
  • yes, ...and I notice a 404 error is return (url not found), however, when process the URL using postman (bearer token selected); I get results from the API. ...Any suggestion? Commented May 11, 2018 at 16:06
  • @ionizer, I was wondering, when processing the url via Postman, I entered a bearer token generated from the a previous API, could the issue be, that I need to pass a token in my code? Commented May 11, 2018 at 16:21

2 Answers 2

2

Your Auth header is wrong

Change

'Authorization': 'abc123def.abc123def.abc123def'

To

'Authorization': 'Bearer abc123def.abc123def.abc123def'
Sign up to request clarification or add additional context in comments.

Comments

1

Try changing the following

[
    {
        "id": 444,
        "ggg_progid": "0000-111-222-333-44444",
        "clientName": "client 1- aaa-bb-01-05 - AA"
    }
]

to

{
  [
      {
        "id": 444,
        "ggg_progid": "0000-111-222-333-44444",
        "clientName": "client 1- aaa-bb-01-05 - AA"
      }
  ]
}

8 Comments

Thanks for the quick reply, unfortunately, I do not have direct access to the API, (The API was provided by another). Is there anything I can do in my code?
I was wondering, when processing the url via Postman, I entered a bearer token generated from the a previous API, could the issue be, that I need to pass a token in my code?
You need to pass access token for sure, yes
I modified the code I posted; I implemented a GET method into the fetch statement and where I have authorization, I entered a token that was generated from postman, but I still get the same error. I'm new to API consumption; could you take a look at my code and let me know what am doing wrong?
Add bearer to authorization header. I posted new answer
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.