Reputation: 1013
I made an API that reads data from a database based on a given id
via a GET request.
It gets some data where id
equals some integer.
I want to call this API in Flutter using package:http/http.dart
but the GET request doesn't allow me to enter a request body (where I would pass in the id
).
I've Googled and Googled to no avail (I may just be googling the wrong thing). I tried changing my API to a POST rather than GET, which works, but I feel it defeats the purpose of GET.
Here is how I do it using POST:
String url = "http://example.com/api/v1";
Map<String, String> headers = {"Content-type": "application/json"};
String json = '{"id": 1}';
Response response = await post(url, headers: headers, body: json);
But can I achieve this with GET in Flutter?
Upvotes: 2
Views: 11630
Reputation: 127024
You can use the Request
class for this:
final request = Request('GET', 'http://example.com/api/v1');
request.body = '{"id": 1}';
final response = request.send().stream.first;
Alternatively, you can also use idiomatic syntax, i.e. standard procedure, and provide your parameter in the URI:
await get('http://example.com/api/v1?id=1', headers: headers, body: json);
Upvotes: 4
Reputation: 1893
There is a dart package that provides some helper classes for http requests.
Github : https://github.com/Ephenodrom/Dart-Basic-Utils
Install it with:
dependencies:
basic_utils: ^1.5.1
It is also part of the EZ-Flutter Collection :
Github : https://github.com/Ephenodrom/EZ-Flutter Docs : https://ez-flutter.de/docs
dependencies:
ez_flutter: ^0.2.5
In your case, you can't send a body within a GET request. You have to set the id in the url you are calling. Usage
Map<String, String> headers = {
"Some": "Header"
};
Map<String, String> queryParameters = {
"Some": "Parameter"
};
String url = "example.com/api/v1/something/;
String id ="100"
url = url + id;
Map<String, dynamic> reaponseBody;
try {
responseBody = await HttpUtils.getForJson(url,
queryParameters: queryParameters, headers: headers);
} catch (e) {
// Handle exception, for example if response status code != 200-299
}
// do something with the response body
Additional information :
These are all methods from the HttpUtils class.
Future<Map<Response> getForFullResponse(String url,{Map<String, dynamic> queryParameters,Map<String, String> headers});
Future<Map<String, dynamic>> getForJson(String url,{Map<String, dynamic> queryParameters,Map<String, String> headers});
Future<String> getForString(String url,{Map<String, dynamic> queryParameters,Map<String, String> headers});
Future<Map<Response> postForFullResponse(String url, String body,{Map<String, String> queryParameters,Map<String, String> headers});
Future<Map<String, dynamic>> postForJson(String url, String body,{Map<String, String> queryParameters,Map<String, String> headers});
Future<String> postForString(String url, String body,{Map<String, String> queryParameters,Map<String, String> headers});
Future<Response> putForFullResponse(String url, String body,{Map<String, String> queryParameters,Map<String, String> headers});
Future<Map<String, dynamic>> putForJson(String url, String body,{Map<String, String> queryParameters,Map<String, String> headers});
Future<String> putForString(String url, String body,{Map<String, String> queryParameters,Map<String, String> headers});
Future<Response deleteForFullResponse(String url,{Map<String, String> queryParameters,Map<String, String> headers});
Future<Map<String, dynamic>> deleteForJson(String url,{Map<String, String> queryParameters,Map<String, String> headers});
Future<String> deleteForString(String url,{Map<String, String> queryParameters,Map<String, String> headers});
Map<String, dynamic> getQueryParameterFromUrl(String url);
String addQueryParameterToUrl(String url, Map<String, dynamic> queryParameters);
Upvotes: -1