I'm currently coding a Swift app and I want to send a request containing a JSON string to my .NET backend/web service. However, when using this code in:
ASP.NET:
String jsonString = Request.Form["jsonString"];
Swift:
let url:NSURL = NSURL(string: "https://www.backend.com/backend.aspx")!;
let request = NSMutableURLRequest(URL: url);
request.HTTPMethod = "POST";
request.HTTPBody = ("jsonString=" + jsonString).dataUsingEncoding(NSUTF8StringEncoding)!;
let session = NSURLSession.sharedSession()
let task = session.dataTaskWithRequest(request) {
(
let data, let response, let error) in
guard let _:NSData = data, let _:NSURLResponse = response where error == nil else {
print("error")
return
}
let dataString = NSString(data: data!, encoding: NSUTF8StringEncoding)
print(dataString)
}
task.resume() }
The string on my asp.net backend turns out to be null. For whatever reason, I can't seem to get the string to be retrieved from the request. Any ideas?
Thanks in advance
I used Alamofire module to handle the POST request.
Alamofire.request(.POST, "someWebSite/api/someApi", parameters: ["Name":"Pancakes", "Price":9.99])
.responseJSON{response in
print("1", response.request) // original URL request
print("2", response.response) // URL response
print("3", response.data) // server data
print("4", response.result) // result of response serialization
if let JSON = response.result.value {
print("JSON: \(JSON)")
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.