skip to Main Content

I’m building an application using flutter where the user provides a string and a set of values must be returned.
I’m unable to figure out what is the cause for the issue.I tried all the solutions provided to the questions similar to this issue but weren’t successful.Any help would be really appreciated.
I converted the actual code to dart only, for easy testing online using dartpad.

import 'dart:convert' as convert;

import 'package:http/http.dart' as http;

final body = <String, String>{
          "id": '1',
          "language": "en",
          "text": "I love this service",
        };

final headers = <String, String>{
      "content-type": "application/json",
      "X-RapidAPI-Key": "7f980b3d2cmsh1d666b571febd6ep11df80jsna27f76c06e6b",
      "X-RapidAPI-Host": "big-five-personality-insights.p.rapidapi.com",
    };
      
void main(List<String> arguments) async {
  final response = await http.post(
    Uri.parse('https://big-five-personality-insights.p.rapidapi.com/api/big5'),
    headers: headers,
    body: [
      convert.jsonEncode(body),
    ],
  );
  if (response.statusCode == 201) {
    // If the server did return a 201 CREATED response,
    // then parse the JSON.
    print('success');
    print(convert.jsonDecode(response.body));
    
  } else {
    // If the server did not return a 201 CREATED response,
    // then throw an exception.
    print('fail');
    throw Exception('Failed to get a response.');
  }
}

2

Answers


  1. The body parameter of a post method sets the body of the request. It can be a String, a List or a Map<String, String>. If it’s a String, it’s encoded using encoding and used as the body of the request. The content-type of the request will default to "text/plain".

    As you passed it as a List, then it expects it to be a List of integers, but you are passing it a List type (or in this specific case List type). Here is a fixed code.

    final response = await http.post(
        Uri.parse('https://big-five-personality-insights.p.rapidapi.com/api/big5'),
        headers: headers,
        body: convert.jsonEncode(body),
    );
    
    Login or Signup to reply.
  2. You have a bad value for the body argument of http.post.

    The documentation for the method states:

    body sets the body of the request. It can be a String, a List or a Map<String, String>. […] If body is a List, it’s used as a list of bytes for the body of the request.

    Since the API you are talking to requires an array to be sent, you want to wrap the body in a list before converting it all to json (note how the brackets have shifted inside the convert method:

      final response = await http.post(
        Uri.parse('https://big-five-personality-insights.p.rapidapi.com/api/big5'),
        headers: headers,
        body: convert.jsonEncode([body]),
      );
    

    Sidenote: The API responds with statusCode 200 on a successful request, not 201; at least in my testing.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search