0

Suppose the host site is: :

https://dev.xyz.com

API header key: "x-api-key: 7462-3172-8773-3312-5819" To register a new user you have to call PUT method: {{host}}/api/customer/ And the body is like this:

{"email": "test@example.net",
"password": "aabbccdd",
"Name": "John",
}

Now how do I accomplish this in flutter? I have searched through several tutorials and still in confusion.

Jack23
  • 1,368
  • 7
  • 32
Ambani saheb
  • 61
  • 1
  • 7

3 Answers3

1

Import the http package from dart library and alias it as http, reason for this aliasing is that you dont want to have .get() method suggestion everywhere in the file. So when you use it with http as http.get() it will give you suggestion for get, In which you can pass the parameter called headers.

Code goes as follows:

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

  url = 'YOUR_URL';
  var response = await http.get(
    url,
    headers: {HttpHeaders.authorizationHeader: TOKEN}, //an example header
  );

In your case,

import 'dart:convert';
import 'dart:io';
import 'dart:async';

main() async {
  String url =
      'https://dev.xyz.com';
  Map map = {
    'data': {'apikey': '7462-3172-8773-3312-5819'},
  };

  print(await apiRequest(url, map));
}

Future<String> apiRequest(String url, Map jsonMap) async {
  HttpClient httpClient = new HttpClient();
  HttpClientRequest request = await httpClient.postUrl(Uri.parse(url));
  request.headers.set('content-type', 'application/json');
  request.add(utf8.encode(json.encode(jsonMap)));
  HttpClientResponse response = await request.close();
  // todo - you should check the response.statusCode
  String reply = await response.transform(utf8.decoder).join();
  httpClient.close();
  return reply;
}
Yudhishthir Singh
  • 2,941
  • 2
  • 23
  • 42
0

You need put the header on the http request. For example:

await put(urlApi + 'update/'+ customer.id, headers: {'token': token, 'content-type': 'application/json'},body: body);
0

You can use http flutter package and your code may be like

//Declared default headers
Map<String, String> headers = {"Accept": "application/json"};
//Passing the url
      Uri url = Uri.parse("https://dev.xyz.com/api/test");
// Make the request, make sure your function has a return type of future and add async keyword in order to use await :)
      var response = await http.post(url,headers: {...headers,'Authorization':"Bearer 7462-3172-8773-3312-5819"},body:{"email": "test@example.net",
"password": "aabbccdd",
"Name": "John",
});
      print(response.body);

in your case if your not using Bearer as your auth keword You can remove the bear key word and use x-api-key keyword so sample will be like

//Declared default headers
Map<String, String> headers = {"Accept": "application/json"};
//Passing the url
      Uri url = Uri.parse("https://dev.xyz.com/api/test");
// Make the request, make sure your function has a return type of future and add async keyword in order to use await :)
      var response = await http.post(url,headers: {...headers,'Authorization':"x-api-key 7462-3172-8773-3312-5819"},body:{"email": "test@example.net",
"password": "aabbccdd",
"Name": "John",
});
      print(response.body);
jasjastone
  • 11
  • 3