I need to send a GET HTTP request with a JSON body. I know that this is not allowed by the RestFul specs. However, there is no chance to change the server. Is there a way to overcome this restriction in Flutter?
This is the code I am trying to use but I couldn't find a way to insert the body.
String urlGetkey = "https://pa.me/transactions/card_hash_key";
Map userHeader = {
"Content-type": "application/json",
"Accept": "application/json",
"User-Agent": "curl/7.64.",
};
var _body = jsonEncode({"api_key": zulu});
var request = http.Request('GET', Uri.parse(urlGetkey));
request.body = _body;
final streamedResponse = await request.send();
var response = await http.Response.fromStream(streamedResponse);
'''
Thanks in advance
You are close, but you need to use a subclass of BaseRequest
so that you can add the body, which you can do by grabbing its sink and adding the body to that before then sending the request.
import 'dart:convert';
import 'package:http/http.dart' as http;
void main() async {
final payload = '{"api_key":"ak_test_46po64VBDm3aqJmR8oXvFLqQi0VxtP"}';
final request = http.StreamedRequest(
'GET',
Uri.https(
'api.pagar.me',
'/1/transactions/card_hash_key',
),
);
request.headers['content-type'] = 'application/json';
request.sink
..add(utf8.encode(payload))
..close();
final streamedResponse = await request.send();
final response = await http.Response.fromStream(streamedResponse);
print(response.statusCode);
print(response.body);
}