我一直在做一个iOS应用,从今天开始,它一直运行良好。
今天我的应用程序开始显示没有数据,当我检查时,我发现当我向服务器发送数据请求时,它说我的请求是一个get请求。在服务器中,我有一个检查,只允许post请求。下面是相关的代码。
if(strcasecmp($_SERVER['REQUEST_METHOD'], 'POST') != 0){
// throw new Exception('Request method must be POST!');
echo json_encode(["status"=> "0", "message"=> "Request must be post request", "Server Var" => $_SERVER], JSON_PRETTY_PRINT);
die();
}
在iOS应用中,我发送了一个post请求。以下是代码:在iOS应用中,我发送了一个post请求。
let param = state ? "\(BASE_URL)user/stateslist.php": "\(BASE_URL)user/countieslist.php"
let dic = ["apikey": APIKEY]
let session = URLSession.shared
let url = URL(string: param)
var request = URLRequest(url: url!)
request.addValue("application/json", forHTTPHeaderField: "Accept")
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
do {
let json = try JSONSerialization.data(withJSONObject: dic, options: .prettyPrinted) // pass dictionary to nsdata object and set it as request body
request.httpBody = json
} catch let error {
print(error.localizedDescription)
}
request.httpMethod = "POST"
let tache = session.dataTask(with: request) { (data, response, error) -> Void in
print(response)
if let antwort = response as? HTTPURLResponse {
let code = antwort.statusCode
print("Code is \(code)")
guard error == nil else {
completion(error, nil)
return
}
guard let data = data else {
completion(error, nil)
return
}
var counties = [StateCounty]()
do {
//create json object from data
if let json = try JSONSerialization.jsonObject(with: data, options: .mutableContainers) as? [String: Any] {
print(json)
if let data = json["data"] as? NSArray{
for user in data{
if let item = user as? NSDictionary{
let user = StateCounty(dic: item)
counties.append(user)
}
}
completion(nil, counties)
}
else{
completion(nil, nil)
}
}
else{
completion(nil, nil)
}
} catch let error {
completion(error, nil)
print(error.localizedDescription)
}
}
}
print("HTTP METHOD \(tache.currentRequest?.httpMethod)")//this says post
tache.resume()
但这是我从API得到的结果。上面提供的代码是为PHP执行的,这是输出。
["message": Request must be post request, "status": 0, "Server Var": {
"DOCUMENT_ROOT" = "/home/u562977278/domains/domain/public_html";
HTTPS = on;
"HTTP_ACCEPT" = "application/json";
"HTTP_ACCEPT_ENCODING" = "gzip, deflate";
"HTTP_ACCEPT_LANGUAGE" = "en-us";
"HTTP_HOST" = "host";
"HTTP_USER_AGENT" = "Gotyour6/1.2.6 CFNetwork/1107.1 Darwin/19.0.0";
"LSPHP_ProcessGroup" = on;
PATH = "/usr/local/bin:/bin:/usr/bin";
"PHP_SELF" = "/gotyour6/user/countieslist.php";
"QUERY_STRING" = "";
"REMOTE_ADDR" = "103.255.5.84";
"REMOTE_PORT" = 7083;
"REQUEST_METHOD" = GET;
"REQUEST_SCHEME" = https;
"REQUEST_TIME" = 1588750834;
"REQUEST_TIME_FLOAT" = "1588750834.773307";
"REQUEST_URI" = "/foldername/user/countieslist.php";
"SCRIPT_FILENAME" = "/home/u562977278/domains/domainname/public_html/gotyour6/user/countieslist.php";
"SCRIPT_NAME" = "/gotyour6/user/countieslist.php";
"SCRIPT_URI" = "https://domainname/gotyour6/user/countieslist.php";
"SCRIPT_URL" = "/gotyour6/user/countieslist.php";
"SERVER_ADDR" = "46.17.175.194";
"SERVER_ADMIN" = "";
"SERVER_NAME" = "servername";
"SERVER_PORT" = 443;
"SERVER_PROTOCOL" = "HTTP/1.1";
"SERVER_SOFTWARE" = LiteSpeed;
"SSL_CIPHER" = "TLS_AES_128_GCM_SHA256";
"SSL_CIPHER_ALGKEYSIZE" = 128;
"SSL_CIPHER_USEKEYSIZE" = 128;
"SSL_PROTOCOL" = "TLSv1.3";
"X-LSCACHE" = on;
"X_SPDY" = HTTP2;
}]
我不知道是客户端的问题还是服务器端的问题。但通过使用Postman进一步调试,我发现在Postman下可以工作,但在iOS下就不行了。同样的请求给出了预期的结果,并且将请求视为POST请求,但在iOS上却不是同样的情况。
希望能在这里了解一下这个问题。
这似乎不是你提供的代码中的http方法的错误.如果你在模拟器上运行应用程序并使用Charles或WireShark跟踪请求,你可以检查实际的http请求。或者在设备上使用Charles或其他代理.你应该看到这样的东西。
POST /user/stateslist.php HTTP/1.1
Host localhost:8080
Content-Type application/json
Accept application/json
User-Agent MyPlayground123456789/1 CFNetwork/1125.2 Darwin/19.4.0
Content-Length 19
Accept-Language en-us
Accept-Encoding gzip, deflate
Connection keep-alive