I am trying to create a simple request to the SMMRY API using swift. I have never made a request to an API and I am fairly new to programming. So far I have:
我正在尝试使用swift创建一个对SMMRY API的简单请求。我从来没有向API发出过请求,而且我对编程也很新。到目前为止我有:
@IBAction func summarizeButtonPressed(_ sender: Any) {
var url : String = "http://api.smmry.com"
var request : NSMutableURLRequest = NSMutableURLRequest()
request.url = NSURL(string: url) as URL?
request.httpMethod = "POST"
but I'm unsure how to finish the function. There are several parameters that can be passed in to the API along with the url including several that are required (API key) but I don't know where to put those. More information on these parameters can be found at http://smmry.com/api. I assume that after that is done I will be returned a JSON array with which I will then have to parse. Is SwiftyJSON recommended for beginners or should I learn how to do it manually?
但我不确定如何完成这个功能。有几个参数可以与url一起传递给API,包括几个必需的参数(API密钥),但我不知道在哪里放这些参数。有关这些参数的更多信息,请访问http://smmry.com/api。我假设在完成之后,我将返回一个JSON数组,然后我必须解析它。 SwiftyJSON是推荐给初学者还是应该学习如何手动完成?
If helping is too much to ask I would appreciate any good tutorials on learning how to make API requests in Swift as well.
如果帮助太多,我会很感激任何关于学习如何在Swift中发出API请求的好教程。
1 个解决方案
#1
0
Well, the question seems too broad...
嗯,问题似乎太宽泛了......
On how to perform the request, you should read about URLSession
.
关于如何执行请求,您应该阅读有关URLSession的信息。
About how to send aditional information in the request, that will depend on how the API expects the parameters. If they are expected as HTTP headers, take a look at addValue(_:forHTTPHeaderField:)
in URLRequest
. If they are expected as part of the body, take a look at the httpBody
property.
关于如何在请求中发送aditional信息,这取决于API如何期望参数。如果需要它们作为HTTP标头,请查看URLRequest中的addValue(_:forHTTPHeaderField :)。如果预期它们是身体的一部分,请查看httpBody属性。
#1
0
Well, the question seems too broad...
嗯,问题似乎太宽泛了......
On how to perform the request, you should read about URLSession
.
关于如何执行请求,您应该阅读有关URLSession的信息。
About how to send aditional information in the request, that will depend on how the API expects the parameters. If they are expected as HTTP headers, take a look at addValue(_:forHTTPHeaderField:)
in URLRequest
. If they are expected as part of the body, take a look at the httpBody
property.
关于如何在请求中发送aditional信息,这取决于API如何期望参数。如果需要它们作为HTTP标头,请查看URLRequest中的addValue(_:forHTTPHeaderField :)。如果预期它们是身体的一部分,请查看httpBody属性。