How to use http.post and send as String Json Object in Flutter

Related searches

I am trying to send a json object to an api end point. But I get the following:

I / flutter(28184): 200
I / flutter(28184): {"error": "the JSON object must be str, not 'bytes'"}

I have tried the following:

Future<http.Response> sendPost() async{
    var post = {
      "profile_id" : "${_profile_id}",
      "profile_name" : "${_profile_Name}",
      "profile_country" : "${_profile_country}",
      "post_id" : "${current_post_id}",
      "post_image_id" : "${current_post_image}",
      "post_desc" : "La La La La La La",
      "post_likes" : "${post_likes}",
      "post_timestamp" : "05-06-2019 22:34",
      "post_comments" : [],
      "post_comments_profile_name" : [],
      "post_comments_profile_image_id" : [],
      "post_comments_timestamp" : []
    };


    var body = utf8.encode(json.encode(post));

    var addPost = await http.post(
      url,
      headers: {"content-type" : "application/json"},
      body: body

      );

      print("${addPost.statusCode}");
      print("${addPost.body}");
      return addPost;
  }

Also when you answer remember that there should be empty arrays inside this Map.

No need to convert the map:

var body = utf8.encode(json.encode(post)); // delete this

Just send the map to the body like this:

var addPost = await http.post(
    url,
    body: post
);

HTTP POST with Json on Body - Flutter/Dart, import 'package:http/http.dart' as http; Map data = { 'key1': 1, 'key2': "some text" } String body = json.encode(data); http.Response response = await http.post( url:� 1. Add Package. In this example, we are using an HTTP dart package for creating an HTTP post request. It is a composable, Future-based library for making HTTP requests. This package contains high-level functions and classes that make it easy to consume HTTP resources.

In order to send the JSON into body, you should stringing the JSON.

var body = JSON.stringify(post);

I think no need to encode.

Make a HTTP `POST` Request with JSON as Body in Dart or Flutter , You can use dart:convert to convert the raw JSON string to objects. See Parsing Simple JSON in Flutter and Dart for help with that. POST request. This guide looks into ways of using JSON with Flutter. It covers which JSON solution to use in different scenarios, and why. Terminology: Encoding and serialization are the same thing—turning a data structure into a string. Decoding and deserialization are the opposite process—turning a string into a data structure.

Do not convert the map by using utf8.encode(). Since the JSON object must be a string and not bytes. Directly send the map :

var addPost = await http.post(
    url,
    body: post
);

I understand from the comment that you want to retrive the string array in future. Do not worry that can be easily achieved :

Link for the same: Flutter how to convert String to List<String>

How to make HTTP requests in Flutter | by Suragch, Learn how to send HTTP requests to APIs using Dart's http library. This easy tutorial will show how to send GET, POST and PUT requests in Flutter. It expects an object (which will be serialized into JSON) or a string. You can� Now replace /posts with /posts/1 in the url. Using /posts returns an array of JSON objects while /posts/1 returns a single JSON object, where 1 is the ID of the post you want to get. You can use

Sending HTTP requests in Dart (Flutter), HTTP GET: Use get for a general request, read for a request returning a string, To encode JSON, pass a simple value (string, boolean, or number literal), or a� I am trying to convert success JSON string to Object. May be I am not doing in proper manner But I have couple of question related to converting JSON to Object. Also I am getting this exeption in Log. NoSuchMethodError: Class '_InternalLinkedHashMap' has no instance method 'cast' with matching arguments.

Making HTTP Networking Requests in Flutter, # New Flutter application $ flutter create flutter_http # Open this up inside of VS Code $ cd flutter_http && code . Adding the HTTP plugin. Head over to your pubspec.yaml and add the following plugin: dependencies: flutter: sdk: flutter http: ^0.12.0+2

Note that the parameters were a Map on the Dart side. Then they were converted to a JSON string by the json.encode() function from the dart:convert library. That string is the POST body. So if the server is asking you to pass it data in a GET request body, check again. While it is possible to design a server in this way, it isn't standard.

Comments
  • Hi, thanks for the reply, I cannot use like that since my map contains string arrays. How can I overcome this?