0

I'm trying to make an get request to the Newscatcher API with the code given below

import 'dart:convert' as convert;

import 'package:http/http.dart' as http;

void main() async {
  var queryParameters = {
    'q':'Tesla',
  };
  
  var headers = {
  'x-api-key': 'apikeyhidden'
};
  
  var url = Uri.https('api.newscatcherapi.com', '/v2/search', queryParameters);
  var response = await http.get(url, headers: headers);
  
  if (response.statusCode == 200) {
    final jsonResponse = convert.jsonDecode(response.body);
    print('$jsonResponse');
  } else {
    print('Reponse error with code ${response.statusCode}');
  }
}

I'm getting the following error code

Response error with code 401

Please help to solve this issue.

I'm using DartPad.

13
  • tried to print ${response.body}? does it have any additional info? Commented Dec 5, 2021 at 6:20
  • 1
    make sure API key is true Commented Dec 5, 2021 at 6:21
  • @MojtabaGhiasi API key is valid and got it from the newscatcher dashboard by creating an account. Commented Dec 5, 2021 at 6:42
  • @pskink I used this else statement to just notify me of the type of error I'm receiving. Commented Dec 5, 2021 at 6:43
  • 1
    I’m voting to close this question because the issue is due to the user providing wrong API KEY. Commented Dec 5, 2021 at 6:58

1 Answer 1

1

API key was the issue. Issue Closed. I used an expired API key. I tried again with a new API and and it worked.

Sign up to request clarification or add additional context in comments.

Comments

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.