RPP
RPP

Reputation: 55

Not Able to fetch response from NSURL in swift

I an uisng swift to fetch response from a url which looks like this http://api.remix.bestbuy.com/v1/categories%28id%3Dpcmcat312300050015%29?apiKey=gj4x2wbttmj3t4tj2ca4dkgu"

The base url is till v1 In the base url i need to append the categories or product or store. After appending this i need to send the categories id like this (id = cat00000). Then question mark and apikey is required to be appended.

I am able to make the url which is probably incorrect var baseUrl: String = "http://api.remix.bestbuy.com/v1/categories" var categoryIdString:String = BestBuyConstants.topCategory var apiString:String = BestBuyConstants.Key

    var urlString: String = "\(baseUrl)" + "\(categoryIdString)"
    println("urlString: \(urlString)")

    let parameterString:String = "gj4x2wbttmj3t4tj2ca4dkgu"
    var url: String = "\(urlString)?apiKey=\(parameterString)"

    var finalURL: NSURL = NSURL(string:url)!
    let request = NSMutableURLRequest(URL: finalURL)

However as i append the category i get fatal error Please suggest where I am doing wrong and some technical documentation to go through.

Upvotes: 2

Views: 139

Answers (1)

Ashish Kakkad
Ashish Kakkad

Reputation: 23902

There is brackets () available in your URL you have not added it :

var baseUrl: String = "http://api.remix.bestbuy.com/v1/categories"
var categoryIdString:String =  "Dpcmcat312300050015"

var urlString: String = "\(baseUrl)" + "(id%3\(categoryIdString))"
println("urlString: \(urlString)")

let parameterString:String = "gj4x2wbttmj3t4tj2ca4dkgu"
var url: String = "\(urlString)?apiKey=\(parameterString)"

println(url)

var finalURL: NSURL = NSURL(string:url)!
let request = NSMutableURLRequest(URL: finalURL)

I have changed at "(id%3\(categoryIdString))" This point.

http://api.remix.bestbuy.com/v1/categories%28id%3Dpcmcat312300050015%29?apiKey=gj4x2wbttmj3t4tj2ca4dkgu

Because check this link if you run it in browser you will find the (%3)

See this link for other encoding in HTML : https://grox.net/utils/encoding.html

Hope it work for you.

Upvotes: 1

Related Questions