0

I am trying to create an Alamofire request as follows:

//creating parameters for the post request
        let parameters: Parameters=[
            "modelo": modelo_id
        ]
        let url_dispo = URL(string: "https://../calcular_precio.php")

        Alamofire.request(url_dispo!, method: .post, parameters: parameters, encoding: JSONEncoding.default)
                        .responseJSON { response in
                            print(response)
            //to get status code
                            if let status = response.response?.statusCode {
                                switch(status){
                                case 201:
                                    print("example success")
                                default:
                                    print("error with response status: \(status)")
                                }
                            }
            //to get JSON return value
                        if let result = response.result.value {
                            let JSON = result as! NSDictionary
                            print(JSON)
                        }

                    }

My issue is that the response from the url is telling me that the post parameter is not sended with the request.

I have checked the value for modelo_id and it is correct.

Is there anything wrong in my code?

Here you have the complete PHP script:

 <?php
require_once 'include/DB_Functions.php';
$db = new DB_Functions();

// json response array
$response = array("error" => FALSE);

if (isset($_POST['modelo']) ) {

    // receiving the post params
    $modelo= $_POST['modelo'];


    // get the user by email and password
    $user = $db->getPrecioByModelo($modelo);

    if ($user != false) {
        // use is found
        $response["error"] = FALSE;
        $response["id"] = $user["id"];
        $response["precio"]["p1"] = $user["p1"];
        $response["precio"]["p2"] = $user["p2"];
        $response["precio"]["p3"] = $user["p3"];
        $response["precio"]["p4"] = $user["p4"];
        $response["precio"]["p5"] = $user["p5"];
        $response["precio"]["p6"] = $user["p6"];
        $response["precio"]["p7"] = $user["p7"];
        $response["precio"]["p8"] = $user["p8"];
        $response["precio"]["p9"] = $user["p9"];
        $response["precio"]["p10"] = $user["p10"];
        $response["precio"]["p11"] = $user["p11"];
        $response["precio"]["p12"] = $user["p12"];
        $response["precio"]["p13"] = $user["p13"];
        $response["precio"]["p14"] = $user["p14"];
        $response["precio"]["p15"] = $user["p15"];
        $response["precio"]["p16"] = $user["p16"];
        $response["precio"]["p17"] = $user["p17"];
        $response["precio"]["p18"] = $user["p18"];
        $response["precio"]["p19"] = $user["p19"];
        $response["precio"]["p20"] = $user["p20"];
        $response["precio"]["p21"] = $user["p21"];
        $response["precio"]["p22"] = $user["p22"];
        $response["precio"]["p23"] = $user["p23"];







        echo json_encode($response);
    } else {

        $response["error"] = TRUE;
        $response["error_msg"] = "Wrong action! Please, try again!";
        echo json_encode($response);
    }
} else {
    // required post params is missing
    $response["error"] = TRUE;
    $response["modelo"] = $modelo;
    $response["error_msg"] = "Required parameters missing!";
    echo json_encode($response);
}
?>

EDIT:

Here you have the debugger output:

modeloid  1069
SUCCESS: {
    error = 1;
    "error_msg" = "Required parameters email or password is missing!";
    modelo = "<null>";
}
error with response status: 200
{
    error = 1;
    "error_msg" = "Required parameters email or password is missing!";
    modelo = "<null>";
}
mvasco
  • 4,965
  • 7
  • 59
  • 120

1 Answers1

0

The problem is that you are sending the data with Alamofire using

JSONEncoding.default

and expecting your data on the backend as URL encoding. Change the encoding to:

URLEncoding.default 

and you should be good to go.

JoeGalind
  • 3,545
  • 2
  • 29
  • 33