How to make HTTP POST request with url encoded body in flutter?

前端 未结 8 1496
南笙
南笙 2020-12-01 03:59

I\'m trying to make an post request in flutter with content type as url encoded. When I write body : json.encode(data), it encodes to plain text.

If I

8条回答
  •  栀梦
    栀梦 (楼主)
    2020-12-01 04:09

    I came here just trying to make an HTTP POST request. Here is an example of how to do that:

    import 'dart:convert';
    import 'package:http/http.dart';
    
    
    makePostRequest() async {
    
      final uri = 'http://httpbin.org/post';
      final headers = {'Content-Type': 'application/json'};
      Map body = {'id': 21, 'name': 'bob'};
      String jsonBody = json.encode(body);
      final encoding = Encoding.getByName('utf-8');
    
      Response response = await post(
        uri,
        headers: headers,
        body: jsonBody,
        encoding: encoding,
      );
    
      int statusCode = response.statusCode;
      String responseBody = response.body;
    }
    

提交回复
热议问题