Swift 5, make http post request
Below is the code for Post
Method,using URLSession
let Url = String(format: "http://10.10.10.53:8080/sahambl/rest/sahamblsrv/userlogin")
guard let serviceUrl = URL(string: Url) else { return }
let parameters: [String: Any] = [
"request": [
"xusercode" : "YOUR USERCODE HERE",
"xpassword": "YOUR PASSWORD HERE"
]
]
var request = URLRequest(url: serviceUrl)
request.httpMethod = "POST"
request.setValue("Application/json", forHTTPHeaderField: "Content-Type")
guard let httpBody = try? JSONSerialization.data(withJSONObject: parameters, options: []) else {
return
}
request.httpBody = httpBody
request.timeoutInterval = 20
let session = URLSession.shared
session.dataTask(with: request) { (data, response, error) in
if let response = response {
print(response)
}
if let data = data {
do {
let json = try JSONSerialization.jsonObject(with: data, options: [])
print(json)
} catch {
print(error)
}
}
}.resume()
}
Send POST request using NSURLSession
You could try using a NSDictionary for the params. The following will send the parameters correctly to a JSON server.
NSError *error;
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:nil];
NSURL *url = [NSURL URLWithString:@"[JSON SERVER"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:60.0];
[request addValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request addValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setHTTPMethod:@"POST"];
NSDictionary *mapData = [[NSDictionary alloc] initWithObjectsAndKeys: @"TEST IOS", @"name",
@"IOS TYPE", @"typemap",
nil];
NSData *postData = [NSJSONSerialization dataWithJSONObject:mapData options:0 error:&error];
[request setHTTPBody:postData];
NSURLSessionDataTask *postDataTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
}];
[postDataTask resume];
Hope this helps (I'm trying to sort a CSRF authenticity issue with the above - but it does send the params in the NSDictionary).
Related Topics
How to Send Email Using Mfmailcomposeviewcontroller in Simulator
Application(...Continue Useractivity...) Method Not Called in iOS 13
How to Draw a Point Using Core Graphics
How to Disable iPhone 6 Native Resolution
Replace a Particular Color Inside an Image with Another Color
How to Get Index Path of Cell on Switch Change Event in Section Based Table View
Delete App from Itunes Connect
User Set Time for Notification in Swift
How to Set Cornerradius for Only Bottom-Left,Bottom-Right and Top-Left Corner of a Uiview
How to Programmatically Add a Uisegmentedcontrol to a Container View
Leak from Nsurl and Avaudioplayer Using Arc
Change the Root View of Uihostingcontroller in Swiftui
Perform a Deeplink from Swiftui Widget on Tap
How to Set an Nscalendarunitminute Repeatinterval on iOS 10 Usernotifications
How to Force Uiviewcontroller Orientation