简体   繁体   English

用Alamofire发布请求

[英]Post request with Alamofire

Hi I'm trying to make a post request with Alamofire using instructions from GitHub. 嗨,我正尝试使用GitHub上的说明向Alamofire发出发布请求。 Could somebody tell me what's wrong with my code, because new data doesn't appear in json ? 有人可以告诉我我的代码出了什么问题,因为新数据没有出现在json中? Here is my VC code. 这是我的VC代码。

import UIKit
import Alamofire

class ViewController: UIViewController {

private struct Constants {

    static let jsonURL3 = "https://jsonblob.com/api/jsonBlob/f31670d2-11bf-11e8-8318-7b18a50c9bd0"
}

override func viewDidLoad() {
    super.viewDidLoad()
    let params = [
        "firstname": "Ivan",
        "lastname": "Nikulin"
    ]

    Alamofire.request(Constants.jsonURL3, method: .post, parameters: params)

}

}

You need to add the responseJSON for your call, you have only done the first part: 您需要为调用添加responseJSON ,仅完成了第一部分:

Alamofire.request(Constants.jsonURL3, method: .get, parameters: params).responseJSON { response in
    print(response.result.value)
}

Secondly this is a get request, so change method from post to get (as I have done in my example). 其次,这是一个get请求,因此将methodpost更改为get (就像我在示例中所做的那样)。

This will print out the following: 这将打印出以下内容:

Optional(<__NSArrayI 0x60c0008371a0>(
{
    firstname = Leo;
    lastname = Messi;
},
{
    firstname = Cristiano;
    lastname = Ronaldo;
}
)
)

Update: 更新:
You can´t post to that URL, you´ll get 405 Method Not Allowed . 您无法发布到该URL,您将得到405 Method Not Allowed If you want to post values you need to create/find an API or URL that allow that, that link is not for that purpose. 如果要发布值,则需要创建/查找允许该值的API或URL,该链接并非用于此目的。

You created a request object of type DataRequest but did not fire it. 您创建了类型为DataRequest的请求对象,但没有触发它。 You need to call responseJSON on that object to get a response. 您需要在该对象上调用responseJSON以获取响应。 Here is example from your code 这是您的代码中的示例

let request = Alamofire.request(Constants.jsonURL3, method: .post, parameters: params)
request.responseJSON { (response) in
    print(response.result.value)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM