代码之家  ›  专栏  ›  技术社区  ›  j.krissa

Alamofire POST请求URL

  •  1
  • j.krissa  · 技术社区  · 7 年前

    假设我有一个URL“ http://aewfwfpi.staginwwedwfg.dewfewf.io/mobile/v1/network/station/ 10 /时间表SS“

    哪里 10 在上面的URL中,是stationId,可以是任何数字。

    如何使用alamofire在URL中发布具有更改参数(StationId)的请求?

    我应该把它添加到参数部分吗? 请帮忙

    目前我静态调用它,如下所示:- 阿拉莫菲尔。请求( " http://aewfwfpi.staginwwedwfg.dewfewf.io/mobile/v1/network/station/ 10 /TimeTabless“, 参数:无, 标题:无)

    2 回复  |  直到 7 年前
        1
  •  2
  •   shayegh    7 年前
    var id = 123
    var yourURL = "http://aewfwfpi.staginwwedwfg.dewfewf.io/mobile/v1/network/station/\(id)/timetablesss"
    

    使用 \(variableName) 在字符串中插入变量或常量

    标题是指定要发送和接收的内容格式的位置。

    标题示例:

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

    参数是放置要发送(POST)的数据的位置

    参数示例:

    let pararameters: [String, Any] = [
                "name": name as String,
                "id": id as Int,
               ]
    
        2
  •  2
  •   SWAT    7 年前

    要执行POST请求,请将参数添加到请求主体,而不是url本身。

    您必须遵循以下内容:

    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
    
        }