قالب وردپرس درنا توس
Home / IOS Development / what I'm doing wrong (HTTP request using URLSession): iOSProgramming

what I'm doing wrong (HTTP request using URLSession): iOSProgramming



I wonder what I'm doing wrong. I'm trying to learn how to use URLSession instead of relying on Alamofire for HTTP requests to yelpApi. When I try the following code, I get the status code 400 for poor request.

Thanks for all the answers.

la configuration = URLSessionConfiguration.default

configuration.waitsForConnectivity = true

] la session = URLSession (configuration: configuration)

guard let url = URL (string: "https: / /api.yelp.com/v3/businesses/search ") otherwise {return}

var urlcomponent = URLComponents (string:" (url) ")

la queryItem = URLQueryItem (name:" location ", value : "NYC")

leave queryItemterm = URLQueryItem (name: "term", value: "restaurant")

urlcomponent? .QueryItems = [queryItemterm,queryItem]

was request = URLRequest (url: url)

request.httpMethod = "GET"

request.addValue ("Bearer (apiKey)", forHTTPHeaderField: "Authorization") [19659004] request.addValue ("application / json", forHTTPHeaderField: "Content-Type")

la task = session.dataTask (with: request) {(data, answer, error) in [19659004] if la response e = answer like? HTTPURLResponse {19659004] print (response)

} other {

print ("error")

}

}

task.resume ()

}

“ 19659026 ]
Source link