I'm trying to update an attribute via PUT request using JSONSerialization.
Here is my function :
func updateRGPDStatus(user:UserModel){
let parameters = ["firstname": user.firsname as Any,
"lastname": user.lastname as Any,
"mail": user.mail as Any,
"has_web_access": user.has_web_access as Any,
"has_mobile_access": user.has_mobile_access as Any,
"id_user": user.id_user as Any,
"has_accepted_rgpd": user.rgpd as Any
] as [String : Any]
guard let url = URL(string: UPDATE_RGPD_STATUS) else { return }
var request = URLRequest(url: url)
request.httpMethod = "PUT"
guard let httpBody = try? JSONSerialization.data(withJSONObject: parameters, options: []) else {
return
}
request.httpBody = httpBody
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
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()
}
and here is the json that I'm supposed to update :
{
"user": {
"firstname": "first_name",
"lastname": "last_name",
"mail": "validEmail#mail.com",
"has_web_access": true,
"has_mobile_access": true,
"id_user": 17,
"has_accepted_rgpd": false
}
}
When I send my request, I got and error of type "missing parameters" because I don't include the "user" attribute in my request which I don't know how to do it at this stage of my learning. Any help please. Thank you ^^
You can try
let res:[String : Any] = ["firstname": user.firsname as Any,
"lastname": user.lastname as Any,
"mail": user.mail as Any,
"has_web_access": user.has_web_access as Any,
"has_mobile_access": user.has_mobile_access as Any,
"id_user": user.id_user as Any,
"has_accepted_rgpd": user.rgpd as Any
]
let parameters:[String : Any] = ["user":res] // the new parameters
I have recently worked with the http put method i sent parameters like this
let param = [
"quantity" : quantity!
] as [String : Any]
And this worked for me.
Related
The JSON I have to post:
{
"quizId": 1,
"quizQuestionBanks": [
{
"quizQuestionBankId": 4,
"question": "string",
"option1": "string",
"option2": "string",
"option3": "string",
"option4": "string",
"answer": "Guy de Maupassant",
"explanation": "string"
}
]
}
I did the URLSession Post part. But don't know how to post this kind of JSON. Earlier I have posted JSON like this.
let json: [String: Any] = ["key": "value"]
But it's a bit complex for me.
The code I did for posting is given below
let url = URL(string: postUrl)! //PUT Your URL
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.setValue("\(String(describing: jsonData?.count))", forHTTPHeaderField: "Content-Length")
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.addValue("Token \(String(describing: token))", forHTTPHeaderField: "Authorization")
// insert json data to the request
request.httpBody = jsonData
URLSession.shared.dataTask(with: request) { data, response, error in
guard let data = data, error == nil else {
print(error?.localizedDescription ?? "No data")
return
}
let responseJSON = try? JSONSerialization.jsonObject(with: data, options: [])
if let responseJSON = responseJSON as? [String: Any] {
print(responseJSON) //Code after Successfull POST Request
}
}.resume()
Prepare JSON data for upload
struct Quiz: Codable {
let quizId: Int
let quizQuestionBanks: [QuizQuestionBank]
}
struct QuizQuestionBank: Codable {
let quizQuestionBankId: Int
let question: String
let option1: String
let option2: String
let option3: String
let option4: String
let answer: String
let explanation: String
}
let quiz = Quiz(quizId: 1, quizQuestionBanks: [QuizQuestionBank(quizQuestionBankId: 4, question: "string", option1: "string", option2: "string", option3: "string", option4: "string", answer: "Guy de Maupassant", explanation: "string")])
guard let uploadData = try? JSONEncoder().encode(quiz) else {
return
}
Configure URL request
let url = URL(string: "https://example.com/post")!
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
Use upload task
let task = URLSession.shared.uploadTask(with: request, from: uploadData) { data, response, error in
if let error = error {
print ("error: \(error)")
return
}
guard let response = response as? HTTPURLResponse,
(200...299).contains(response.statusCode) else {
print ("server error")
return
}
if let mimeType = response.mimeType,
mimeType == "application/json",
let data = data,
let dataString = String(data: data, encoding: .utf8) {
print ("got data: \(dataString)")
}
}
task.resume()
See this link from Apple developer site for more detailed information.
https://developer.apple.com/documentation/foundation/url_loading_system/uploading_data_to_a_website
With JSONSerialization:
let params: [String: Any] = ["quizId": 1,
"quizQuestionBanks": [["quizQuestionBankId": 4,
"question": "string",
"option1": "string",
"option2": "string",
"option3": "string",
"option4": "string",
"answer": "Guy de Maupassant",
"explanation": "string"]]]
let jsonData = try? JSONSerialization.data(withJSONObject: params)
But, since Swift 4, we can use Codable:
struct Quiz: Codable {
let id: Int
let questions: [Question]
enum CodingKeys: String, CodingKey {
case id = "quizId"
case questions = "quizQuestionBanks"
}
}
struct Question: Codable {
let id: Int
let question: String
let option1: String
let option2: String
let option3: String
let option4: String
let answer: String
let explanation: String
enum CodingKeys: String, CodingKey {
case id = "quizQuestionBankId"
case question
case option1
case option2
case option3
case option4
case answer
case explanation
}
}
let questions = [Question(id: 4, question: "string", option1: "string", option2: "string", option3: "string", option4: "string", answer: "Guy de Maupassant", explanation: "string")]
let quiz = Quiz(id: 1, questions: questions)
let jsonData = try JSONEncoder().encode(quiz)
option1, option2, option3, option4, could be an array in struct (that would need a custom encoding/decoding).
The API i have is a post method and response in postman is as follows:
Post Url : https://xxxxxx/xxxxxxxx/public/api/chatrequest
Body params:property_id:40 , identity:demo Header: x-token and content type
The response is as follows:
{
"Token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiIsImN0eSI6InR3aWxpby1mcGE7dj0xIn0.eyJqdGkiOiJTS2E4ZGNjNTRlZTkwODYyNDcyNWRiN2Q4ODlhZjY3YTJkLTE1ODY2MjAyMDIiLCJpc3MiOiJTS2E4ZGNjNTRlZTkwODYyNDcyNWRiN2Q4ODlhZjY3YTJkIiwic3ViIjoiQUNjNTllMWMzN2ZmZjdiZmIyYTIxMzkyOGY3NTdjMzhkYyIsImV4cCI6MTU4NjYyMzgwMiwiZ3JhbnRzIjp7ImlkZW50aXR5IjoiMTciLCJjaGF0Ijp7InNlcnZpY2Vfc2lkIjoiSVMxODQ1Mzg5ZDhmYjE0N2M4OTU2Zjg5ZmM0MTk1ODdkYSJ9fX0.FeLxlmdYr8WglNs_2vH0qYGbeRJ9vaVoWIVE8qTn6hA",
"identity": 17,
"ChannelID": "jNqNLaUS2mUg1TRqMVPua5ghcFDpBSUI",
"ChannelSid": "CHb24932d10fe34bca82153c87ba18edc1",
"ChannelData": {
"id": 76,
"propertys_id": "40",
"from_user": "17",
"to_user": "18",
"channel": "jNqNLaUS2mUg1TRqMVPua5ghcFDpBSUI",
"sid": "CHb24932d10fe34bca82153c87ba18edc1",
"from_token": "MB368a9a6ea1e64078abaa98b61952eb25",
"to_token": "MB15aea3092366476e8ab437d303fbdab5",
"created_at": "2020-04-10 17:01:52",
"updated_at": "2020-04-10 17:01:52"
}
}
The code used to fetch this response is as follows:
fetchToken(params: ["property_id":"21"]) {response,error in
var token: String?
token = response["token"] as? String
var clientsid: String?
clientsid = response["ChannelSid"] as? String
completion(token != nil, token,clientsid)
}
func fetchToken(params:[String:String], completion:#escaping (NSDictionary, NSError?) -> Void) {
var request = URLRequest(url: URL(string: "https://reachwebdemo.com/listcrib/public/api/chatrequest")!)
request.httpMethod = "POST"
let postString = postDataFrom(params: params)
print("poststring is",postString)
request.httpBody = postString.data(using: .utf8)
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
request.setValue(self.usertoken, forHTTPHeaderField: "x-token")
let task = URLSession.shared.dataTask(with: request) { data, response, error in
guard let data = data, error == nil else {
print("error=\(String(describing: error))")
completion(NSDictionary(), error as NSError?)
return
}
if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode != 200 {
completion(NSDictionary(), NSError(domain: "TWILIO", code: 1000, userInfo: [NSLocalizedDescriptionKey: "Incorrect return code for token request."]))
return
}
do {
let json = try JSONSerialization.jsonObject(with: data, options: []) as! [String:Any]
print("json response = \(json)")
completion(json as NSDictionary, error as NSError?)
} catch let error as NSError {
completion(NSDictionary(), error)
}
}
task.resume()
}
else {
let userInfo = [NSLocalizedDescriptionKey : "TokenRequestUrl Key is missing"]
let error = NSError(domain: "app", code: 404, userInfo: userInfo)
completion(NSDictionary(), error)
}
}
The response i get from the code is as follows:
json response = ["errors": <__NSSingleObjectArrayI 0x600003faebf0>(
The property id field is required.
)
]
What is the issue with the code?Why is the response is not fetched properly same as postman?
Depending on the Json data, if json data (success = true), then only I want to move the user to next View Controller.
{
"success": true,
"message": "Loged In",
"User_details": {
"user_id": "208",
"user_name": "Samad",
"phone": "9705994458",
"email": "laddafsamad.12#gmail.com",
"image": "https://laex.in/testprep/Dashboard/uploads/user/1499063695.jpg"
}
}
if let responce = yourJson as? [String: Any],
let isSuccess = responce["success"] as? Bool,
isSuccess {
// Send user to next viewcontroller
}
Try this code :)
let result : NSDictionary! = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as! NSDictionary
if let success = result.value(forKey: "success") as? Bool {
if success {
// navigate to next viewcontroller
}
}
I have an issue with loading JSON results within swift (php connection).
I can retrieve JSON data but it will not let me assign it to a variable.
it always assigns the results as Optional.
The JSON Data:
{
"country": [{
"id": 1,
"name": "Australia",
"code": 61
}, {
"id": 2,
"name": "New Zealand",
"code": 64
}]
}
The xCode Output:
["country": <__NSArrayI 0x60000002da20>(
{
code = 61;
id = 1;
name = Australia;
},
{
code = 64;
id = 2;
name = "New Zealand";
}
)
]
Country Name: Optional(Australia)
Country Name: Optional(New Zealand)
The .swift file:
//function did_load
override func viewDidLoad() {
super.viewDidLoad()
//created RequestURL
let requestURL = URL(string: get_codes)
//creating NSMutable
let request = NSMutableURLRequest(url: requestURL!)
//setting the method to GET
request.httpMethod = "GET"
//create a task to get results
let task = URLSession.shared.dataTask(with: request as URLRequest) {
data, response, error in
if error != nil{
print("error is \(String(describing: error))")
return;
}
//lets parse the response
do {
let json = try JSONSerialization.jsonObject(with: data!, options: .allowFragments) as! [String: Any]
print(json)
if let countries = json["country"] as? [[String: AnyObject]] {
for country in countries {
print("Country Name: \(String(describing: country["name"]))")
print("Country Code: \(String(describing: country["code"]))")
if let couname = country["name"] as? [AnyObject] {
print(couname)
}
if let coucode = country["code"] as? [AnyObject] {
print(coucode)
}
}
}
} catch {
print("Error Serializing JSON: \(error)")
}
}
//executing the task
task.resume()
}
You need to unwrap the optional before you try to use it via string interpolation. The safest way to do that is via optional binding:
Please use below code, which will work for you.
if let countries = json["country"] as? [[String: AnyObject]] {
for country in countries {
print("Country Name: \(country["name"] as! String)")
print("Country Code: \(country["code"] as! String)")
if let couname = country["name"] as? String {
print(couname)
}
if let coucode = country["code"] as? Int {
print(coucode)
}
}
}
I am new in Swift and iOS. Please send me the sample code for POST request with JSON data to external database for login app in iOS using Swift
You can call post api like below code,
Try this
func callPostApi()
{
let baseUrl : NSString = NSString(format: "YOUR_BASE_URL") //"http://at.webby.com/php.api"
let request = NSMutableURLRequest(URL: NSURL(string: baseUrl as String)!)
let session = NSURLSession.sharedSession()
request.HTTPMethod = "POST"
let stringPost = "username=test&password=12345" // Key and Value param as string
let data = stringPost.dataUsingEncoding(NSUTF8StringEncoding)
request.timeoutInterval = 60
request.HTTPBody=data
let task = session.dataTaskWithRequest(request, completionHandler: {data, response, error -> Void in
let err1: NSError? = nil
do
{
let json = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers)
print(json)
}
catch
{
print(err1)
}
})
task.resume()
}
Hope this will help you.
class ViewController: UIViewController {
#IBOutlet weak var tblView: UITableView!
var arrData = NSArray()
override func viewDidLoad() {
super.viewDidLoad()
postAPICall()
}
func postAPICall() {
let urlString = "http://18.220.215.90/backend/web/index.php/site/getstylistlist"
let dictParameter = ["Salon_Latitude": "23.0387822",
"Register_User_ID": "0",
"page": "0",
"Filter_Keyword": "",
"Register_User_Year_Of_Experience": "0",
"Salon_Longitude": "72.514567",
"pagesize": "20",
"api_type": "iphone",
"Register_User_Login_ID": "0",
"Cost": "0",
"Version": "1",
"api_userid": "",
"Distance_KM": "",
"Register_User_Professional_Sub_Type": "All"
]
let param = JsonStringFromDictionary(parameter: dictParameter as AnyObject, type: "json")
Alamofire.request(urlString, method: .post, parameters: param, encoding: URLEncoding.httpBody, headers: nil).responseJSON { (response:DataResponse!) in
switch response.result{
case.success:
print("Sucess")
if let JSON = response.result.value {
self.arrData = JSON as! NSArray
print(self.arrData)
}
case.failure(let Error):
print("error\(Error)")
}
}
}