In flutter I tried to POST data using the DIO package, I posted some data:

final response = await dio.post(
      "127.0.0.1/toko_grosir_api/create.php",
      data: {
        'name': '${event.product.nameProduct}',
        'price': '${event.product.priceProduct}',
        'desc': '${event.product.descProduct}',
        'stock': '${event.product.stockProduct}',
        'date': '${event.product.dateProduct}',
        'category': '${event.product.categoryProduct}'
      },
      options: Options(
        contentType: Headers.formUrlEncodedContentType,
      ),
    );

and I am trying to receive data using PHP as Rest API with the following code:

$nameProd = isset($_POST['name']) ? $_POST['name'] : null;
$priceProd = isset($_POST['price']) ? $_POST['price'] : null;
$descProd = isset($_POST['desc']) ? $_POST['desc'] : null;
$stockProd = isset($_POST['stock']) ? $_POST['stock'] : null;
$dateProd = isset($_POST['date']) ? $_POST['date'] : null;
$categoryProd = isset($_POST['category']) ? $_POST['category'] : null;

However, it displays an error when POSTing the data as like this:

This exception was thrown because the response has a status code of 404 and RequestOptions.validateStatus was configured to throw for this status code. The status code of 404 has the following meaning: "Client error - the request contains bad syntax or cannot be fulfilled" Read more about status codes at https://developer.mozilla.org/en-US/docs/Web/HTTP/Status In order to resolve this exception you typically have either to verify and fix your request code or you have to fix the server code.

I've tried the PHP rest API using postman and it worked according to the variables needed in the rest API, what's wrong with with my code?

0

There are 0 best solutions below