Post request with JSON body in Swift - swift

I have been looking for a similar solution for a long time but I can't find it.
I have to make a post request for my Stripe server.
For that I have to send a request with a JSON in Body : {"price" : 50}.
I can't figure out what's wrong.
Thank you
override func viewDidLoad() {
super.viewDidLoad()
checkoutButton.addTarget(self, action: #selector(didTapCheckoutButton), for: .touchUpInside)
checkoutButton.isEnabled = false
let parameters: [String: Any] = [
"price": "50",
]
let jsonData = try? JSONSerialization.data(withJSONObject: parameters)
var request = URLRequest(url: backendCheckoutUrl)
request.httpMethod = "POST"
request.setValue("\(String(describing: jsonData?.count))", forHTTPHeaderField: "Content-Length")
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
request.httpBody = jsonData
let task = URLSession.shared.dataTask(with: request, completionHandler: { [weak self] (data, response, error) in
guard let data = data,
let json = try? JSONSerialization.jsonObject(with: data, options: []) as? [String : Any],
let customerId = json["customer"] as? String,
let customerEphemeralKeySecret = json["ephemeralKey"] as? String,
let paymentIntentClientSecret = json["paymentIntent"] as? String,
let publishableKey = json["publishableKey"] as? String,
let self = self else {
return
}
STPAPIClient.shared.publishableKey = publishableKey
var configuration = PaymentSheet.Configuration()
configuration.merchantDisplayName = "Example, Inc."
configuration.applePay = .init(
merchantId: "com.foo.example", merchantCountryCode: "US")
configuration.customer = .init(
id: customerId, ephemeralKeySecret: customerEphemeralKeySecret)
configuration.returnURL = "....."
configuration.allowsDelayedPaymentMethods = true
self.paymentSheet = PaymentSheet(
paymentIntentClientSecret: paymentIntentClientSecret,
configuration: configuration)
DispatchQueue.main.async {
self.buyButton.isEnabled = true
}
})
task.resume()
}

Related

Swift POST Request Method Not Allowed

I Use Laravel as backend and I have below route to verify the users
$router->post('SignIn','Api\V1\UserProfileController#SignIn');
I have tested this route many time using postman and its working fine, now i want to send post request from my app using below request
let url = URL(string: "http://192.168.xxx.xxx/BARI/public/Api/V1/Verify")!
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
let param : [String : Any] = ["ph_number" : userDefaults.string(forKey: "ph_number")!, "code" : smsNumberTF.text!]
request.httpBody = try? JSONSerialization.data(withJSONObject: param, options: [])
let configuration = URLSessionConfiguration.default
configuration.timeoutIntervalForRequest = TimeInterval(30)
configuration.timeoutIntervalForResource = TimeInterval(30)
let session = URLSession(configuration: configuration)
let task = session.dataTask(with: url) { (data, urlResponse, error) in
if(error != nil){
DispatchQueue.main.async {
self.progress.stopAnimating()
self.isLoading = false
// show connection error alert
print("connection error : \(error?.localizedDescription)")
}
}else{
let outputStr = String(data: data!, encoding: String.Encoding.utf8) as String?
print(outputStr)
DispatchQueue.main.async {
do {
self.progress.stopAnimating()
self.isLoading = false
let jsonData = try JSONDecoder().decode(BasicResponse.self, from: data!)
if(jsonData.statusCode == 1000){
// let userDefaults = UserDefaults.standard
// userDefaults.set("+964" + self.phoneET.text!, forKey: "contact_number")
// let vc = Verfiy()
// self.navigationController?.pushViewController(vc, animated: true)
}else{
//self.alert.show(target: self.view, message: jsonData.message!)
}
}
catch let jsonerr {
print("error serrializing error",jsonerr)
}
}
}
}
task.resume()
But Im getting Method Not Allowed response back? what Im missing her!?
Any Help will be much appreciated

How do i pass the data from my parser out the function

I need to pass the data of the "object" in my Parser outside the function.
I tried to make a multidim array and store it to it but i can only access it inside the function, it comes empty outside when i print it.
ApiParser.parseFuction(strUrl: Constants.URL.Global + "HR/EndOfDay/GetAllEndOfReports", method: "GET", token: "", params: [String : AnyObject]()) { (status, object) in
let data = object["value"] as! [[String: AnyObject]]
let jsonData = try? JSONSerialization.data(withJSONObject: data)
let test = try! JSONDecoder().decode([EODDataContainer].self, from: jsonData!)
print(test)
} // this is the code inside the viewdidload
class ApiParser: NSObject {
class func parseFuction(strUrl: String, method: String, token: String, params: [String : AnyObject]?, postCompleted: #escaping (_ statusCode: Int, _ object: [String: AnyObject]) ->()) {
let className = "--- ApiParser: ------->>>"
let url = URL(string: strUrl)
var request = URLRequest(url: url!)
request.httpMethod = method
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.addValue("Bearer \(token)", forHTTPHeaderField: "Authorization")
if method == "POST" || method == "PUT" {
let jsonData = try? JSONSerialization.data(withJSONObject: params!)
request.httpBody = jsonData
}
let sessionConfig = URLSessionConfiguration.default
sessionConfig.timeoutIntervalForRequest = 300.0
sessionConfig.timeoutIntervalForResource = 300.0
let session = URLSession(configuration: sessionConfig)
session.dataTask(with: request) { (data, response, err) in
print(request)
guard let httpResponse = response as? HTTPURLResponse else { return }
let statusCode = httpResponse.statusCode
print("statusCode ---->>> \(statusCode)")
DispatchQueue.main.async {
guard let data = data else { return }
do {
let json = try JSONSerialization.jsonObject(with: data, options: .mutableContainers) as! [String: AnyObject]
postCompleted(statusCode, json)
} catch let jsonErr {
print("Serializing jsonError \(type(of: jsonErr))", jsonErr._code)
postCompleted(jsonErr._code, [String: AnyObject]())
}
}
}.resume()
}
} //this is my api parser class
I want to store the "test" variable inside the "twoDimensionalArray"
In the ViewController class :
var test : [EODDataContainer]?
override func ViewDidLoad() {
super.viewDidLoad()
ApiParser.parseFuction(strUrl: Constants.URL.Global + "HR/EndOfDay/GetAllEndOfReports", method: "GET", token: "", params: [String : AnyObject]()) { (status, object) in
let data = object["value"] as! [[String: AnyObject]]
let jsonData = try? JSONSerialization.data(withJSONObject: data)
self.test = try! JSONDecoder().decode([EODDataContainer].self, from: jsonData!)
print(test)
}

Send Codable Object To Alamofire

I have the following custom class.
class FundingRequest: Codable {
let dispatcherName, loadReference, origin, destination: String
let pickedUp, droppedOff, driverFirstName, driverLastName: String
let grossTruckPay, advancesTaken, fundingMethod, comments: String
let additionalCharges, rateConfirmation, billOfLading, lumperReceipt: [String]
let salesTicket: [String]
enum CodingKeys: String, CodingKey {
case dispatcherName = "DispatcherName"
case loadReference = "LoadReference"
case origin = "Origin"
case destination = "Destination"
case pickedUp = "PickedUp"
case droppedOff = "DroppedOff"
case driverFirstName = "DriverFirstName"
case driverLastName = "DriverLastName"
case grossTruckPay = "GrossTruckPay"
case advancesTaken = "AdvancesTaken"
case fundingMethod = "FundingMethod"
case comments = "Comments"
case additionalCharges = "AdditionalCharges"
case rateConfirmation = "RateConfirmation"
case billOfLading = "BillOfLading"
case lumperReceipt = "LumperReceipt"
case salesTicket = "SalesTicket"
}
init(dispatcherName: String, loadReference: String, origin: String, destination: String, pickedUp: String, droppedOff: String, driverFirstName: String, driverLastName: String, grossTruckPay: String, advancesTaken: String, fundingMethod: String, comments: String, additionalCharges: [String], rateConfirmation: [String], billOfLading: [String], lumperReceipt: [String], salesTicket: [String]) {
self.dispatcherName = dispatcherName
self.loadReference = loadReference
self.origin = origin
self.destination = destination
self.pickedUp = pickedUp
self.droppedOff = droppedOff
self.driverFirstName = driverFirstName
self.driverLastName = driverLastName
self.grossTruckPay = grossTruckPay
self.advancesTaken = advancesTaken
self.fundingMethod = fundingMethod
self.comments = comments
self.additionalCharges = additionalCharges
self.rateConfirmation = rateConfirmation
self.billOfLading = billOfLading
self.lumperReceipt = lumperReceipt
self.salesTicket = salesTicket
}
}
I need to be able to send this to my web service which expects it as JSON in a parameter named JSON. Also need to pass a secret called 'code' along with this. Not sure how to acheive this using alamofire.
This is what I've tried so far but it didn't work, I'm not doing it right at all.
r fundingReqeust = FundingRequest(dispatcherName: txtDispatcherName.text!, loadReference: txtLoadReference.text!, origin: txtOrigin.text!, destination: txtDestination.text!, pickedUp: txtPickedUp.text!, droppedOff: txtDroppedOff.text!, driverFirstName: txtDriverFirstName.text!, driverLastName: txtDriverLastName.text!, grossTruckPay: txtGrossTruckPay.text!, advancesTaken: "0", fundingMethod: "1", comments: txtComments.text!, additionalCharges: additionalChargesString, rateConfirmation: rateConfirmationsURL, billOfLading: billOfLadingsURL, lumperReceipt: lumperReceiptsURL, salesTicket: SaleTicketURL)
let jsonEncoder = JSONEncoder()
let jsonData = try? jsonEncoder.encode(fundingReqeust)
let json:String = String(data: jsonData!, encoding: String.Encoding.utf8)! // the data will be converted to the string
let urlString = "https://xxx.xxx.com"
let parameters: Parameters = ["code": "xxx", "JSON": json]
Alamofire.request(urlString, method: .post, parameters: parameters,encoding: JSONEncoding.default, headers: nil).responseJSON {
response in
switch response.result {
case .success:
print(response)
break
case .failure(let error):
print(error)
}
}
}
Does your server accepting the data in the format ["code": "xxx", "JSON": json]? I am not sure about it but I think it is like ["code": "xxx", "key1": "val1", "key2" : "val2"]
Try with:
let jsonEncoder = JSONEncoder()
var jsonData = try? jsonEncoder.encode(fundingReqeust) as? [String: Any]
jsonData?["code"] = "xxx"
let urlString = "https://xxx.xxx.com"
Alamofire.request(urlString, method: .post, parameters: jsonData, encoding: JSONEncoding.default, headers: nil).responseJSON {
response in
}
Try the below code :
func postRatingReviewDetail(ratings:RatingFeedbackModal, completion:#escaping RatingFeedbackCompletionHandler){
let urlString = WEBAPI.rating
guard let url = URL(string: urlString) else { return }
let request = NSMutableURLRequest(url: url)
let authString = "Bearer " + TTUserDefalut.getAccessToken()!
let session = URLSession.shared
request.httpMethod = "POST"
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.addValue("application/json", forHTTPHeaderField: "Accept")
request.addValue(authString, forHTTPHeaderField: "Authorization")
do {
**let jsonEncoder = JSONEncoder()
let jsonData = try jsonEncoder.encode(ratings)
request.httpBody = jsonData**
}
catch let error{
print("Error", error)
return
}
let _ = session.dataTask(with: request as URLRequest) { data, response, error in
guard let data = data else { return }
do {
let responseData = try JSONDecoder().decode(RatingResponse.self, from: data)
completion(responseData,response,error)
}
catch{
completion(nil,nil,nil)
}
}.resume()
}

urlrequest not sending post request

Hi i am new to IOS App developement.
My code is
func sendRequest<T: Decodable>(api: String, parameters: [String: String]? = nil, outputBlock: #escaping (T) -> () ) {
guard let url = URL(string: "http://xxyyzz.com/appRegister.php") else {return}
print("hitting : -", url.absoluteString)
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.addValue("application/json", forHTTPHeaderField: "Accept")
let newparam = ["name": "rr", "pass": "123456", "email": "rr#rr.com", "passConfirm":"123456"]
let httpBody = try? JSONSerialization.data(withJSONObject: newparam)
request.httpBody = httpBody
if let data = request.httpBody, let str = String(data: data, encoding: String.Encoding.utf8) {
print(str)
}
URLSession.shared.dataTask(with: request as URLRequest) { (data, response, error) in
DispatchQueue.main.async {
Indicator.shared.hideProgressView()
if let err = error {
print(err.localizedDescription)
return
}
guard let data = data else {return}
do {
let obj = String(data: data, encoding: String.Encoding.utf8)
print(obj ?? "oberrrrr")
}
}
}.resume()
}
and console printed result as per code is below
hitting : - http://xxyyzz.com/appRegister.php
{"email":"rr#rr.com","passConfirm":"123456","name":"rr","pass":"123456"}
{"error":"Please enter all fields."}
url and parameters works well on postman that means their is something missing in my code.
just to answer the problem if anyone else faces this.
this code is fine but the problem was with php web-service as the backend developer was not accepting json values as parameter instead form data was need to send.
So, two types of fix can be made here
accept json at backend by adding :-
$postdata = file_get_contents("php://input");
$request = json_decode($postdata, true);
send form data instead json
func sendRequest<T: Decodable>(api: String, parameters: [String: Any]? = nil, outputBlock: #escaping (T) -> () ) {
guard let url = URL(string: api) else {return}
print("hitting : -", url.absoluteString)
var request = URLRequest(url: url)
if let parameters = parameters {
request.httpMethod = "POST"
var postArr = [String]()
for(key, value) in parameters
{
postArr.append(key + "=\(value)")
}
let postString = postArr.map { String($0) }.joined(separator: "&")
request.httpBody = postString.data(using: .utf8)
if let data = request.httpBody, let str = String(data: data, encoding: String.Encoding.utf8) {
print(str)
}
}
URLSession.shared.dataTask(with: request) { (data, response, error) in
DispatchQueue.main.async {
Indicator.shared.hideProgressView()
if let err = error {
print(err.localizedDescription)
return
}
guard let data = data else {return}
do {
let obj = try JSONDecoder().decode(T.self, from: data)
outputBlock(obj)
} catch let jsonErr {
print(jsonErr)
}
}
}.resume()
}

How can i convert String to Dictionary to access nested array and dictionary, i want to access stateId and stateName to add in to my table

i
func callAddWithPOST(Name mname:String, PhoneNo mphone:String, Email memail:String, Comment mcomments:String){
let login = ["countryId":"1"]
print("Your Result is : = \(login)")
let url = NSURL(string: "http://photokeeper.mgtcloud.co.uk/commonwebservice.asmx/getStateList")!
let session = NSURLSession.sharedSession()
let request = NSMutableURLRequest(URL: url)
do {
let auth = try NSJSONSerialization.dataWithJSONObject(login, options: .PrettyPrinted)
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
request.HTTPMethod = "POST"
request.HTTPBody = auth
let task = session.dataTaskWithRequest(request, completionHandler: { (data, response, error) -> Void in
let responseString = NSString(data: data!, encoding: NSUTF8StringEncoding)
let result = self.convertStringToDictionary(responseString!)
let keys = Array(result!.values)
print(keys[0])
print("Done.")
})
task.resume()
} catch {
print("Error")
}
}
output
{"result":[{"stateId":3871,"stateName":"Aberdeenshire"},{"stateId":3872,"stateName":"Anglesey/Sir Fon"},{"stateId":3873,"stateName":"Angus"},{"stateId":3874,"stateName":"Antrim"},{"stateId":3875,"stateName":"Argyll And Bute"},{"stateId":3876,"stateName":"Armagh"},{"stateId":3877,"stateName":"Ayrshire"},{"stateId":3878,"stateName":"Bedfordshire"},{"stateId":3879,"stateName":"Berkshire"},{"stateId":3880,"stateName":"Blaenau Gwent/Blaenau Gwent"},{"stateId":3881,"stateName":"Bristol"},{"stateId":3882,"stateName":"Buckinghamshire"},{"stateId":3883,"stateName":"Caerphilly/Caerffili"},{"stateId":3884,"stateName":"Cambridgeshire"},{"stateId":3885,"stateName":"Cardiff/Caerdydd"},{"stateId":3886,"stateName":"Cardiganshire/Ceredigion"},{"stateId":3888,"stateName":"Carmarthenshire/Sir Gaerfyrddin"},{"stateId":3890,"stateName":"Cheshire"},{"stateId":3891,"stateName":"Clackmannanshire"},{"stateId":3893,"stateName":"Conwy/Conwy"},{"stateId":3895,"stateName":"County Durham"},{"stateId":3896,"stateName":"Cumbria"},{"stateId":3897,"stateName":"Denbighshire/Sir Ddinbych"},{"stateId":3898,"stateName":"Derbyshire"},{"stateId":3899,"stateName":"Devon"},{"stateId":3901,"stateName":"Dorset"},{"stateId":3902,"stateName":"Down"},{"stateId":3904,"stateName":"Dumfries And Galloway"},{"stateId":3905,"stateName":"Dunbartonshire"},{"stateId":3906,"stateName":"Dundee"},{"stateId":3907,"stateName":"Durham/North Yorkshire"},{"stateId":3908,"stateName":"East Lothian"},{"stateId":3909,"stateName":"East Sussex"},{"stateId":3910,"stateName":"East Yorkshire"},{"stateId":3911,"stateName":"Edinburgh"},{"stateId":3912,"stateName":"Essex"},{"stateId":3913,"stateName":"Fermanagh"},{"stateId":3914,"stateName":"Fife"},{"stateId":3915,"stateName":"Flintshire/Sir Fflint"},{"stateId":3917,"stateName":"Glamorgan/Morgannwg"},{"stateId":3918,"stateName":"Glasgow"},{"stateId":3919,"stateName":"Gloucestershire"},{"stateId":3920,"stateName":"Gwynedd/Gwynedd"},{"stateId":3921,"stateName":"Hampshire"},{"stateId":3922,"stateName":"Herefordshire"},{"stateId":3923,"stateName":"Hertfordshire"},{"stateId":3924,"stateName":"Highland"},{"stateId":3925,"stateName":"Kent"},{"stateId":3929,"stateName":"Lanarkshire"},{"stateId":3930,"stateName":"Lancashire"},{"stateId":3932,"stateName":"Leicestershire"},{"stateId":3935,"stateName":"Lincolnshire"},{"stateId":3936,"stateName":"London"},{"stateId":3937,"stateName":"Londonderry"},{"stateId":3940,"stateName":"Manchester"},{"stateId":3943,"stateName":"Merthyr Tydfil/Merthyr Tydfil"},{"stateId":3944,"stateName":"Midlothian"},{"stateId":3946,"stateName":"Monmouthshire/Sir Fynwy"},{"stateId":3947,"stateName":"Moray"},{"stateId":3948,"stateName":"Neath Port Talbot"},{"stateId":3949,"stateName":"Newport"},{"stateId":3950,"stateName":"Norfolk"},{"stateId":3951,"stateName":"Northamptonshire"},{"stateId":3952,"stateName":"Northumberland"},{"stateId":3953,"stateName":"Nottinghamshire"},{"stateId":3955,"stateName":"Orkney"},{"stateId":3956,"stateName":"Oxfordshire"},{"stateId":3957,"stateName":"Pembrokeshire/Sir Benfro"},{"stateId":3958,"stateName":"Perth And Kinross"},{"stateId":3959,"stateName":"Powys/Powys"},{"stateId":3960,"stateName":"Renfrewshire"},{"stateId":3962,"stateName":"Rutland"},{"stateId":3963,"stateName":"Scottish Borders"},{"stateId":3964,"stateName":"Shetland Isles"},{"stateId":3965,"stateName":"Shropshire"},{"stateId":3967,"stateName":"Somerset"},{"stateId":3968,"stateName":"South Yorkshire"},{"stateId":3969,"stateName":"Staffordshire"},{"stateId":3970,"stateName":"Stirling"},{"stateId":3971,"stateName":"Suffolk"},{"stateId":3972,"stateName":"Surrey"},{"stateId":3973,"stateName":"Swansea"},{"stateId":3975,"stateName":"Torfaen"},{"stateId":3976,"stateName":"Tyrone"},{"stateId":3977,"stateName":"Warwickshire"},{"stateId":3979,"stateName":"West Lothian"},{"stateId":3980,"stateName":"West Midlands"},{"stateId":3981,"stateName":"West Sussex"},{"stateId":3982,"stateName":"West Yorkshire"},{"stateId":3983,"stateName":"Western Isles"},{"stateId":3987,"stateName":"Wiltshire"},{"stateId":3988,"stateName":"Worcestershire"},{"stateId":3989,"stateName":"Wrexham"}],"status":"success"}
Done.
Instead of converting data to String try to convert it Dictionary direct
let task = session.dataTaskWithRequest(request, completionHandler: { (data, response, error) -> Void in
let response = try NSJSONSerialization.JSONObjectWithData(data!, options:.MutableContainers) as! [String: AnyObject]
let status = response["status"] as! String
if status == "success" {
let array = response["result"] as! [[String: AnyObject]]
print(array)
}
})
Note: You have already written your code inside do block thats why i have not declare that block for try, you just need to replace your task block with my one.
Edit:
func callAddWithPOST(Name mname:String, PhoneNo mphone:String, Email memail:String, Comment mcomments:String){
let login = ["countryId":"1"]
print("Your Result is : = \(login)")
let url = NSURL(string: "http://photokeeper.mgtcloud.co.uk/commonwebservice.asmx/getStateList")!
let session = NSURLSession.sharedSession()
let request = NSMutableURLRequest(URL: url)
do {
let auth = try NSJSONSerialization.dataWithJSONObject(login, options: .PrettyPrinted)
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
request.HTTPMethod = "POST"
request.HTTPBody = auth
let task = session.dataTaskWithRequest(request, completionHandler: { (data, response, error) -> Void in
let response = try NSJSONSerialization.JSONObjectWithData(data!, options:.MutableContainers) as! [String: AnyObject]
let status = response["status"] as! String
if status == "success" {
let array = response["result"] as! [[String: AnyObject]]
print(array)
}
})
task.resume()
} catch let error as NSError {
print(error.localizedDescription)
}
}