Home > Blockchain >  Flutter HTTP POST request / code 400 error
Flutter HTTP POST request / code 400 error

Time:08-24

I am trying to make a POST request and I keep getting a 400 error code. I cant figure out why. I have everything I need and it seems to all be in order. Here is the code where I am making the call:

                                  TextButton(
                                    style: TextButton.styleFrom(
                                        primary: Colors.black,
                                        minimumSize: Size(130.w, 40.h)),
                                    onPressed: () async {
                                      await ApiService().createNewTag(textController.text, companyId);
                                      // print(companyId);
                                      // print(textController.text);

                                      Get.toNamed(RouteHelper.getTagsProfile(
                                          int.parse(companyId)));
                                    },
                                    child: Text(
                                      'Save',
                                      style: FlutterFlowTheme.of(context)
                                          .bodyText1
                                          .override(
                                            fontFamily: 'Heebo',
                                            fontSize: 18.sp,
                                            fontWeight: FontWeight.w500,
                                          ),
                                    ),
                                  )),

Here is the code for my POST API call:

    Future createNewTag(String tag, String companyId) async {
    final createTagUrl =
        Uri.parse(AppConstants.BASE_URL   AppConstants.CREATE_NEW_TAG_URI);
    final response = await http.post(createTagUrl, headers: {
      'Authorization': 'Bearer '   AppConstants.TOKEN,
      'X-API-KEY': AppConstants.API_KEY
    }, body: {
      'company_id': companyId,
      'tag': tag
    });
    print(response.statusCode);
    print(response.body);
    return json.decode(response.body);
  }

CodePudding user response:

So after a time of reflecting upon my own sanity and searching the internet, I came upon a thought. If this is not being interpreted by the API correctly, it could potentially cause me to get the error that I am. So I decided to change the way I was sending the body portion of my POST api call. I also thought to try to json.encode the body before it being sent. Low and behold, I found the solution!

    Future createNewTag(String tag, String companyId) async {
    final Map body = {'company_id': companyId, 'tag': tag};
    final createTagUrl =
        Uri.parse(AppConstants.BASE_URL   AppConstants.CREATE_NEW_TAG_URI);
    final response = await http.post(createTagUrl, body: json.encode(body), headers: {
      'Authorization': 'Bearer '   AppConstants.TOKEN,
      'X-API-KEY': AppConstants.API_KEY
    });
    print(createTagUrl);
    print(response.statusCode);
    print(response.body);
    return json.decode(response.body);
  }
  • Related