import SwiftUI
import Alamofire
struct TestView: View {
// Function to send the POST request using Alamofire
func sendPostRequest() {
// URL and data
let url = ""
let parameters: [String: Any] = [
"UserName": "",
"Password": "",
"__RequestVerificationToken": "",
"submitbut": "Submit"
]
let headers: HTTPHeaders = [
"Host": "",
"Content-Type": "application/x-www-form-urlencoded",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.6778.86 Safari/537.36",
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7",
"Referer": "",
"Cookie": "__RequestVerificationToken=_"
]
// Making the POST request with Alamofire
AF.request(url, method: .post, parameters: parameters, encoding: URLEncoding.default, headers: headers)
.validate()
.response { response in
if let response = response.response {
print("Response Status Code: (response.statusCode)")
}
switch response.result {
case .success(let data):
if let str = String(data: data ?? Data(), encoding: .utf8) {
print("Response: (str)")
}
case .failure(let error):
print("Error: (error)")
}
}
}
i removed my requesttoken, website and user details.. when server response printed, always appear as get rrequest response
i have ensured all my cookies are correct, but for some reason im still getting get request reply… have tried many ways, including adding a slash at the end of the url but to no avail.
when i ran this same command in curl bash, i got the correct response to the post request but for some reason on swift when i run this, i keep on getting the get response back
when i debug w alamofirelog, i get this
Request Method: POST
but response is still what i get from a GET request
Lucas Chin is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
3