且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将curl命令转换为iOS

更新时间:2023-02-16 20:48:16

正文不用于GET http方法。使用以下代码连接您的参数:

 扩展字符串{

///百分比转义值要添加到RFC 3986中指定的URL查询值
///
///此百分比转义所有字符包含字母数字字符集和 - ,。,_ ,和〜。
///
/// http://www.ietf.org/rfc/rfc3986.txt
///
///:返回:返回前缀转义字符串。

func stringByAddingPercentEncodingForURLQueryValue() - >串? {
let characterSet = NSMutableCharacterSet.alphanumericCharacterSet()
characterSet.addCharactersInString(-._〜)

return self.stringByAddingPercentEncodingWithAllowedCharacters(characterSet)
}

}

扩展字典{

///构建键和对象的HTTP参数字典的字符串表示
///
///此百分比转义符合RFC 3986
///
/// http://www.ietf.org/rfc/rfc3986.txt
///
///:returns:以key1 = value1& key2 = value2的形式表示的字符串,其中键和值是百分比转义

func stringFromHttpParameters() - > String {
let parameterArray = map(self){(key,value) - >
中的字符串let percentEscapedKey =(key as!String).stringByAddingPercentEncodingForURLQueryValue()!
let percentEscapedValue =(value as!String).stringByAddingPercentEncodingForURLQueryValue()!
return\(percentEscapedKey)= \(percentEscapedValue)
}

return join(&,parameterArray)
}

}


var urlString =https://api.semantics3.com/test/v1/products
var reqText = [upc: 70411576937]
var err:NSError?

let parameterString = reqText.stringFromHttpParameters()
let requestURL = NSURL(string:\(urlString)?\(parameterString))!

var request = NSMutableURLRequest(URL:NSURL(string:urlString)!)
var response:NSURLResponse?
var error:NSError?

request.HTTPMethod =GET
request.addValue(MYAPIKEY,forHTTPHeaderField:api_key)
request.addValue(application / json,forHTTPHeaderField: Content-Type)
request.addValue(application / json,forHTTPHeaderField:Accept)
var session = NSURLSession.sharedSession()
pre>

部分编辑:SWIFT 2.1(更新)

  extension Dictionary {

func stringFromHttpParameters() - > String {
let parameterArray = self.map {(key,value) - >
中的字符串let percentEscapedKey =(key as!String).stringByAddingPercentEncodingForURLQueryValue()!
let percentEscapedValue =(value as!String).stringByAddingPercentEncodingForURLQueryValue()!
return\(percentEscapedKey)= \(percentEscapedValue)
}

return parameterArray.joinWithSeparator(&)
}

}


The following curl works

curl -G -H "api_key: MYAPIKEY" https://api.semantics3.com/test/v1/products -d 'q={"upc":"70411576937"}'

However, upon trying to convert it to iOS I get the following error:

Error Domain=NSURLErrorDomain Code=-1005 "The network connection was lost." {NSErrorFailingURLStringKey=https://api.semantics3.com/test/v1/products,...}

I have attached my code below but I believe that my problem is the "q=" right before the json data that is being submitted to the URL. If so, what is this called and how do I put "q=" before my json data? I can't exactly tell though, due to iOS' unfaltering ability to provide us with unrelated error messages. Thank you.

    var urlString = "https://api.semantics3.com/test/v1/products"
    var request = NSMutableURLRequest(URL: NSURL(string: urlString)!)
    var response: NSURLResponse?
    var error: NSErrorPointer = nil
    var reqText = ["upc": "70411576937"]
    var err: NSError?

    request.HTTPBody = NSJSONSerialization.dataWithJSONObject(reqText, options: nil, error: &err)
    request.HTTPMethod = "GET"
    request.addValue("MYAPIKEY", forHTTPHeaderField: "api_key")
    request.addValue("application/json", forHTTPHeaderField: "Content-Type")
    request.addValue("application/json", forHTTPHeaderField: "Accept")
    var session = NSURLSession.sharedSession()

    var task = session.dataTaskWithRequest(request, completionHandler: {data, response, error -> Void in
        var strData = NSString(data: data, encoding: NSUTF8StringEncoding)
        if(err != nil) {
            println(err!.localizedDescription)
        }
        else {
            //this is where the error is printed
            println(error)
            var parseError : NSError?
            // parse data
            let unparsedArray: AnyObject? = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.AllowFragments, error: &parseError)
            println(parseError)
            if let resp = unparsedArray as? NSDictionary {
                println(resp)
            }
        }
    })
    task.resume()

Body is not used in GET http methods. Use the following code to concat your params:

extension String {

    /// Percent escape value to be added to a URL query value as specified in RFC 3986
    ///
    /// This percent-escapes all characters besize the alphanumeric character set and "-", ".", "_", and "~".
    ///
    /// http://www.ietf.org/rfc/rfc3986.txt
    ///
    /// :returns: Return precent escaped string.

    func stringByAddingPercentEncodingForURLQueryValue() -> String? {
        let characterSet = NSMutableCharacterSet.alphanumericCharacterSet()
        characterSet.addCharactersInString("-._~")

        return self.stringByAddingPercentEncodingWithAllowedCharacters(characterSet)
    }

}

extension Dictionary {

    /// Build string representation of HTTP parameter dictionary of keys and objects
    ///
    /// This percent escapes in compliance with RFC 3986
    ///
    /// http://www.ietf.org/rfc/rfc3986.txt
    ///
    /// :returns: String representation in the form of key1=value1&key2=value2 where the keys and values are percent escaped

    func stringFromHttpParameters() -> String {
        let parameterArray = map(self) { (key, value) -> String in
            let percentEscapedKey = (key as! String).stringByAddingPercentEncodingForURLQueryValue()!
            let percentEscapedValue = (value as! String).stringByAddingPercentEncodingForURLQueryValue()!
            return "\(percentEscapedKey)=\(percentEscapedValue)"
        }

        return join("&", parameterArray)
    }

}


var urlString = "https://api.semantics3.com/test/v1/products"
var reqText = ["upc": "70411576937"]
var err: NSError?

let parameterString = reqText.stringFromHttpParameters()
let requestURL = NSURL(string:"\(urlString)?\(parameterString)")!

var request = NSMutableURLRequest(URL: NSURL(string: urlString)!)
var response: NSURLResponse?
var error: NSError?

request.HTTPMethod = "GET"
request.addValue("MYAPIKEY", forHTTPHeaderField: "api_key")
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.addValue("application/json", forHTTPHeaderField: "Accept")
var session = NSURLSession.sharedSession()

PARTIAL EDIT: SWIFT 2.1 (updated)

extension Dictionary {

    func stringFromHttpParameters() -> String {
        let parameterArray = self.map { (key, value) -> String in
            let percentEscapedKey = (key as! String).stringByAddingPercentEncodingForURLQueryValue()!
            let percentEscapedValue = (value as! String).stringByAddingPercentEncodingForURLQueryValue()!
            return "\(percentEscapedKey)=\(percentEscapedValue)"
        }

        return parameterArray.joinWithSeparator("&")
    }

}