Reputation: 6547
I am creating a swift application where I need to POST data to my own webserver.
At the moment, my swift code looks like this.
fileprivate class AlamofireHelper {
static var Manager : Alamofire.SessionManager = {
// Create the server trust policies
let serverTrustPolicies: [String: ServerTrustPolicy] = [
"192.168.1.243": .disableEvaluation
]
// Create custom manager
let configuration = URLSessionConfiguration.default
configuration.httpAdditionalHeaders = Alamofire.SessionManager.defaultHTTPHeaders
let man = Alamofire.SessionManager(
configuration: URLSessionConfiguration.default,
serverTrustPolicyManager: ServerTrustPolicyManager(policies: serverTrustPolicies)
)
return man
}()
}
class UploadHelper
{
private var _postData = [String:Any]()
private let almoHelper = AlamofireHelper.Manager
init(username:String) {
_postData["username"] = username
print("Setup")
submitRequest()
}
func submitRequest()
{
print("Data is: \(_postData)")
almoHelper.request("http:/192.168.1.243/networkStorage", method: .post, parameters: _postData, encoding: JSONEncoding.default).responseString { (responce) in
print("Responce: \(responce)")
}
}
}
PHP:
<?php
echo "POST: " . var_dump($_POST) . "\n\n\n\n";
echo "GET: " . var_dump($_GET);
?>
The only problem is, when I run the swift code let helper = UploadHelper(username: iProgram)
, I get the below response from my PHP script:
Data: ["username": "iProgram"]
Responce: SUCCESS: array(0) {
}
POST:
array(0) {
}
GET:
Why are my POST and GET variables empty? I understand that the GET is empty becase I am only sending POST data, but shouldnt the POST show somthing like "username = iProgrammer"?
Finaly, here is the HTTP request I captured from burp suit:
GET /networkStorage/ HTTP/1.1
Host: 192.168.1.243
Accept: */*
User-Agent: Network%20Storage/1 CFNetwork/808.2.16 Darwin/16.3.0
Accept-Language: en-gb
Accept-Encoding: gzip, deflate
Connection: close
Edit: For some reason, when I use the below code and change my PHP script to show the 'GET' variable, I can see the data which it should send.
almoHelper.request("http://192.168.1.243/networkStorage", method: .get, parameters: _postData).responseString { (myResponce) in
let result = myResponce.result
print("Result is: \(result.value!)")
}
And here is the request I intercepted from burp suite:
GET /networkStorage/?username=iProgram HTTP/1.1
Host: 192.168.1.243
Accept: */*
User-Agent: Network%20Storage/1 CFNetwork/808.2.16 Darwin/16.3.0
Accept-Language: en-gb
Accept-Encoding: gzip, deflate
Connection: close
Upvotes: 1
Views: 523
Reputation: 11340
First of all, var_dump
doesn't return anything, it does output result, so use
echo "POST: "; var_dump($_POST); echo "\n\n\n\n";
or
echo "POST: " . print_r($_POST, true) . "\n\n\n\n";
GET /networkStorage/ HTTP/1.1
shows that you make GET request, not POST. In addition you have wrong encoding JSONEncoding.defaul
. Try to change request code a little:
almoHelper.request("http:/192.168.1.243/networkStorage", method: .post, parameters: _postData, encoding: URLEncoding.httpBody).responseString { (responce) in
print("Responce: \(responce)")
}
If this does not work, try changing the URL from http:/192.168.1.243/networkStorage
to http:/192.168.1.243/networkStorage/
or http:/192.168.1.243/networkStorage/index.php
.
This might fix the issue when you send a request to /networkStorage
, it sends a new request to /networkStorage/
as shown in the captured data from burpsuit.
Upvotes: 1
Reputation: 99523
Why are my POST and GET variables empty? I understand that the GET is empty becase I am only sending POST data, but shouldnt the POST show somthing like "username = iProgrammer"?
No. $_POST
is only populated for HTML form-like encoding, such as application/x-www-form-urlencoded or multipart/form-data. It looks like what you're sending is JSON, in which case you need to read the php://input
stream to get to the data.
Upvotes: 0