Search code examples
mongodbflutterdart

MongoDB Data Api not working. It returns 400 Bad request


I am trying to insert a document into MongoDB using the Data Api. I have been trying to do this for 2 Days and I have not gotten anywhere. It keeps on telling me 400 Bad request.

When I make a post request to find list of documents, it works. But when I want to deleteOne, updateOne, findOne, insertOne, and insertMany, I get 400 Bad request.

I have given my Api key read and write access. I don't know what to do.

This is how i call the function:

  await MongoDB.insertData(collection: 'PostedJobs', document: {   
    //All other fields are strings besides the ones I commented on   
              "nick_name": "",
              "image_url": '',
              "username": myUserName,
              "title": title,
              'searchCriteria': list, // list string
              "description": description,
              'category': category, // this is map of <string,string>
              "pushToken": "",
              "time": DateTime.now().toIso8601String(),
              "from": "posted",
              "location": location,
              "city": city.replaceAll("State", "").trim(),
              "country": countryName,
              "area": area,
              'latlng': null,
              'budget': !hasBudget ? 0 : int.tryParse(budget!), 
              'seen': [],
            }).then((_) {
              EasyLoading.dismiss();
              Get.back();
            });

This is the function:

static insertData({
    required String collection,
    required document,
  }) async {
    await http
        .post(
            Uri.parse(
                'https://data.mongodb-api.com/app/myappid/endpoint/data/v1/action/insertOne'),
            headers: {
              'Content-Type': "application/json",
              'api-key': APIKEY,
            },
            body: json.encode({
              "dataSource": "MyCluster",
              "database": "Database",
              "collection": collection, //PostedJobs
              "document": document, //The document parameter I passed
            }))
        .then((value) {
      if (value.statusCode == 200)
        print('ok');
      else {
        print(value.request);
        throw ErrorDescription(value.reasonPhrase!);
      }
    }, onError: (c, v) {
      print(c);
      print(v);
    });
  }

In addition: I use the aforementioned functions in Firebase cloud functions and MongoDB trigger functions (Typescript) and it's ok but in dart itself I can't seem to find the problem. Please I need an extra pair of eyes.

Below is MongoDB's example:

curl --request POST \
  'https://data.mongodb-api.com/app/<Data API App ID>/endpoint/data/v1/action/insertOne' \
  --header 'Content-Type: application/json' \
  --header 'api-key: <Data API Key>' \
  --data-raw '{
      "dataSource": "Cluster0",
      "database": "todo",
      "collection": "tasks",
      "document": {
        "status": "open",
        "text": "Do the dishes"
      }
  }'

Solution

  • With the help of Selimdogan. I have been able to move foward. I had to go down to the Base Client to resolve my issue. This is the link:

    flutter: Content-type must be application/json