1
votes

Suppose i have a URL "http://aewfwfpi.staginwwedwfg.dewfewf.io/mobile/v1/network/station/10/timetablesss"

where 10 in the above URL is the stationId and can be any number .

How can i POST a request with changing parameter(StationId) in the URL using alamofire?

Should i add it in Parameter section? Please help

Currently am calling it statically like below:- Alamofire.request( "http://aewfwfpi.staginwwedwfg.dewfewf.io/mobile/v1/network/station/10/timetablesss", parameters:nil, headers: nil)

2

2 Answers

2
votes
var id = 123
var yourURL = "http://aewfwfpi.staginwwedwfg.dewfewf.io/mobile/v1/network/station/\(id)/timetablesss"

use \(variableName) to insert variables or constants in a string

Headers is where you specify the format of the content that you want to send and receive.

example of headers:

 let headers: HTTPHeaders = [
            "Accept": "application/json",
            "Content-Type": "application/json"
           ]

Parameters is where you put the data that you want to send (POST)

example of parameters:

let pararameters: [String, Any] = [
            "name": name as String,
            "id": id as Int,
           ]
2
votes

To do a POST request, add the parameters to the body of the request and not the url itself.

Something like below is what you must follow:

let parameters: Parameters = ["parameter-name": parameterValue]
let headers    = ["Content-Type":"application/json"]
Alamofire.request("<your post url>",method: .post, parameters: parameters, encoding: JSONEncoding.default,headers: headers)
        .responseJSON { response in
            print(response)
            // Do anything you like with the response here

    }