Home > Net >  Making http get requests with headers and query parameters in dart
Making http get requests with headers and query parameters in dart

Time:12-08

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.

CodePudding user response:

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

  • Related