Header in an http.send request

Asked

Viewed 222 times

0

I have a request by stream which is as follows:.

Future<Stream<Chamado>> getChamado() async{
final String url = 'www.api'
final client = new http.Client();
final streamRest = await client.send(
      http.Request('get',Uri.parse(url))
      );
 return streamRest.stream
  .transform(utf8.decoder)
  .transform(json.decoder)
  .expand((data) => (data as List))
  .map((data) => Chamado.fromJSON(data));
}

How do I place a header in this request, using the client.send unseen client.get? has as ?

1 answer

2


The class Request owns the headers as attribute, just create it and add these parameters in the Map.

final request = http.Request('get', Uri.parse('url'));
request.headers['nome'] = 'parâmetro';
request.headers[HttpHeaders.authorizationHeader] = 'Basic ${token}';

final client = http.Client();
final stream = await client.send(request);

Having the request completed, just use the client and client.send() as you are already doing.

  • The only thing I modified to work was the authorization header. Instead of : request.headers[HttpHeaders.authorizationHeader] = 'Basic ${token}'; I put : request.headers['Authorizarion'] = 'Bearer ${token}';

Browser other questions tagged

You are not signed in. Login or sign up in order to post.