Solved: Making http get requests with headers and query parameters in dart

Question

Asked by gokg4 on December 05, 2021 (source).

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.

Answer

Question answered by gokg4 (source).

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

DART FLUTTER HTTP-HEADERS HTTPREQUEST HTTPRESPONSE
SHARE: