45021

How to use PUT request in Alamofire

Question:

I am new in swift and I am also trying to use Alamofire to call data from API. I am quite puzzled on how I will use the <strong>PUT Request</strong> to update data. I've read some solutions here in SO but I don't know how I will apply on my app. I am creating an Event App, the scenario should be, when the participant clicked the <strong>Check In</strong> Button, it will update the registered_flag to true meaning the participant will marked as <em>Registered</em> and the button will be changed to <strong>Check Out</strong>. I really don't know if my API Service is correct or not. Hope you could help me. Thank you so much.

<strong>JSON of the Event Participant Where in the registered_flag should be updated once checkInOutButton</strong>

{ "event_name": "Q & A", "event_participants": [ { "participant_id": "70984656-92bc-4c36-9314-2c741f068523", "employee_number": null, "last_name": "Surname", "first_name": "FirstName", "middle_name": null, "display_name": "Surname, FirstName ", "department_name": "Medical Informatics", "position_name": "Application Developer", "registered_flag": true, "registered_datetime": "2018-09-13T08:54:40.150", "registration_type": 1, "delete_flag": false, "manual_reg_flag": false, "out_flag": false, "out_datetime": null, "classification": 6, "others": "Guest" } }

<strong>JSON to update for check in</strong>

{ "registered_flag": true, "registration_type": 1 }

<strong>updateType</strong>

enum UpdateParticipantType: String { case checkIn = "Check In" case checkOut = "Check Out" }

<strong>APIService for UpdateParticipant</strong>

func updateParticipant(updateType: UpdateParticipantType, participantID: String, successBlock: @escaping ([Attendee]) -> Void, failureBlock: @escaping (Error) -> Void) { let updateParticipantURL = URL(string: "\(REGISTER_PARTICIPANT_URL)/\(updateType)/\(participantID)") Alamofire.request(updateParticipantURL!, method: .put).responseJSON { (response) in print(response) if let error = response.error { failureBlock(error) print(error) return } if let jsonArray = response.result.value as? [[String : Any]] { for anItem in jsonArray { if let eventparticipants = anItem["event_participants"] as? [[String : Any]] { var extractedAttendees = [Attendee]() for participants in eventparticipants{ let success = Attendee.init(JSON: participants) extractedAttendees.append(success!) } successBlock(extractedAttendees) } } } } }

Answer1:

As per <a href="https://github.com/Alamofire/Alamofire/blob/master/Documentation/Usage.md#http-methods" rel="nofollow">Alamofire</a> documentation:

let parameters: Parameters = [ "foo": "bar", "baz": ["a", 1], "qux": [ "x": 1, "y": 2, "z": 3 ] ] Alamofire.request("https://httpbin.org/post", method: .post, parameters: parameters)

For a given json

{ "registered_flag": true, "registration_type": 1 } let parameters: Parameters = [ "registered_flag": true "registration_type": 1 ]

Recommend

  • Plots with a common x axis
  • Dao method returns List while I need a Map
  • How to make a single element in a not sortable (jquery ui)
  • Join two DataFrames with no common columns for calculations
  • x86 PIC, is it correct for QEMU to raise interrupts on all CPUs?
  • Doctrine 2.1 - Relation Lost After ManyToMany Cascade Merge - Symfony2
  • Pandas weird behavior using .replace() to swap values
  • how to check whether a video or audio file format is playable in iphone programatically?
  • Will HttpClient Async methods run in new threads
  • rvest: extract tables with url's instead of text
  • CodeIgniter shared data between calls to load->view
  • Calling JavaScript function in WebView
  • How to echo values of an associative array returned by function
  • Plugin with id Android not found
  • Upload video and extract thumbnail using Amazon s3
  • How to render a react component on any other page other than index.html
  • Iterating over mmaped gzip file with boost
  • Docker 1.12.1: after swarm init, workers unable to join swarm
  • AJAX calls - where does logic go?
  • this.refs is undefined for shallow tests using enzyme on React native
  • Timeseries Crossvalidation in R: using tsCV() with tslm()-Models
  • Encode string to Base64 in Inno Setup (Unicode Version of Inno Setup)
  • ClientRequest, how to serialize POJO's to json data correctly? could not find writer for conten
  • Highcharts Drawing a line with chart.renderer.path
  • Django non-rel createsuperuser fails due to non-ascii characters
  • Forward slash vs backward slash for file path in git bash
  • Mask Image to Video Frame similar as MV Master
  • How to make infinite threaded comments
  • Preloading webView doesn't work - trying to reduce loading time
  • Holoviews plot not rendered in cell in JupyterLab
  • Target in barchart in dc.js
  • Custom progress dialog not working
  • Please update your Node runtime to version >=0.12.x
  • Get name of days between two date in ios?
  • Why is ordered choice in pyparsing failing for my use case?
  • Neo4j…how to get a visual representation of my data?
  • Background transfer download task failed when app was closed
  • Write to .csv file with PHP (Commas in Data Error)