Reputation: 2503
I am using Alamofire in iOS 9 and am noticing that Alamofire is not sending the parameters to my PHP script in a POST method.
This is the PHP script:
if($_SERVER['REQUEST_METHOD'] != 'POST') {
echo '{"status": "error", "message" : "Only POST supported. You sent: '.$_SERVER['REQUEST_METHOD'].'"}';
return;
}
if (!isset($_POST['GENDER']) || !isset( $_POST['CHAR_COUNT_LOWER'] ) || !isset( $_POST['CHAR_COUNT_UPPER'] )) {
echo '{"status": "error", "message" : "Minimum parameters not set."}';
return;
}
Then, when firing my Alamofire POST request, I get the message:
swift:55 fetchData(_:completionHandler:): Only POST supported. You sent: GET
And, finally, this is my Alamofire request:
Alamofire.request(.POST, url, parameters: ["CHAR_COUNT_LOWER":String(lowerValue), "CHAR_COUNT_UPPER":String(upperValue), "GENDER": String(gender!)] )
.validate()
.responseJSON { response in
QL1(response.description)
switch response.result {
case .Success:
var result = [BabyNames]()
let json = JSON(data: response.data!)
if json["status"] == "error" {
QL4(json["message"])
completionHandler(babyNames: [], error: StoreError.CannotFetch("Could not retrieve baby names"))
return
}
.......
I also tried changing the encoding to everything else but JSON, as my service does not take JSON as input. It's a simple service that reads $_POST["param"]
Anything I am doing wrong?
Thanks.
EDIT:
Still going crazy about it. Installed Charles to check what was the request coming out of my PC and here it goes:
EDIT 2:
Change the Alamofire request encoding to JSON:
Alamofire.request(.POST, url, parameters: parameters!, encoding: ParameterEncoding.JSON )
Now I can see a GET and a POST request. The Post request:
URL http://example.com/backend/names/query_names.php Status Complete Response Code 301 Moved Permanently
Upvotes: 0
Views: 1147
Reputation: 2503
Finally found what the problem was.
My request URL was set to http://server.com/api
When I changed it to http://www.server.com/api
That got it working. I don't know honestly why that would happen and if that's something to do with the .httpaccess but that's it.
Upvotes: 3