Shanu
Shanu

Reputation: 657

How to send raw data in flutter http

I wanted to send raw data in flutter http and the data doesn't look like JSON

Here's how I done that in Postmanenter image description here

and tried this in flutter using http,

 Response res = await post(
      Uri.parse(baseUrl + endPoint),
      headers: {'Client-ID': clientId, 'Authorization': 'Bearer $accessToken'},
      body: jsonEncode('fields *'),
    );

and got this in console,

Error: XMLHttpRequest error.

Upvotes: 1

Views: 1254

Answers (1)

Davis
Davis

Reputation: 1427

Add it as this

var headers = {
  'Accept': 'application/json',
  'Content-Type': 'text/plain',
  
};
var request = http.Request('POST', Uri.parse('Your url'));
request.body = '''fields *''';
request.headers.addAll(headers);

http.StreamedResponse response = await request.send();

if (response.statusCode == 200) {
  print(await response.stream.bytesToString());
}
else {
  print(response.reasonPhrase);
}

Or you can easily see it being implemented in Postman's code request to the right just select the code icon and choose http-Dart

Upvotes: 4

Related Questions