swiftui api calling github

var url : String = "http://google.com?test=toto&test2=titi"
var request : NSMutableURLRequest = NSMutableURLRequest()
request.URL = NSURL(string: url)
request.HTTPMethod = "GET"

NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue(), completionHandler:{ (response:NSURLResponse!, data: NSData!, error: NSError!) -> Void in
    var error: AutoreleasingUnsafeMutablePointer<NSError?> = nil
    let jsonResult: NSDictionary! = NSJSONSerialization.JSONObjectWithData(data, options:NSJSONReadingOptions.MutableContainers, error: error) as? NSDictionary

    if (jsonResult != nil) {
        // process jsonResult
    } else {
       // couldn't load JSON, look at error
    }


})

4.2
10

                                    let params = [&quot;username&quot;:&quot;john&quot;, &quot;password&quot;:&quot;123456&quot;] as Dictionary&lt;String, String&gt;

var request = URLRequest(url: URL(string: &quot;http://localhost:8080/api/1/login&quot;)!)
request.httpMethod = &quot;POST&quot;
request.httpBody = try? JSONSerialization.data(withJSONObject: params, options: [])
request.addValue(&quot;application/json&quot;, forHTTPHeaderField: &quot;Content-Type&quot;)

let session = URLSession.shared
let task = session.dataTask(with: request, completionHandler: { data, response, error -&gt; Void in
    print(response!)
    do {
        let json = try JSONSerialization.jsonObject(with: data!) as! Dictionary&lt;String, AnyObject&gt;
        print(json)
    } catch {
        print(&quot;error&quot;)
    }
})

task.resume()

4.2 (10 Votes)
0
4.33
9
Sudheesh 105 points

                                    For GET Request

 let configuration = NSURLSessionConfiguration .defaultSessionConfiguration()
    let session = NSURLSession(configuration: configuration)


    let urlString = NSString(format: &quot;your URL here&quot;)

    print(&quot;get wallet balance url string is \(urlString)&quot;)
    //let url = NSURL(string: urlString as String)
    let request : NSMutableURLRequest = NSMutableURLRequest()
    request.URL = NSURL(string: NSString(format: &quot;%@&quot;, urlString) as String)
    request.HTTPMethod = &quot;GET&quot;
    request.timeoutInterval = 30

    request.addValue(&quot;application/json&quot;, forHTTPHeaderField: &quot;Content-Type&quot;)
    request.addValue(&quot;application/json&quot;, forHTTPHeaderField: &quot;Accept&quot;)

    let dataTask = session.dataTaskWithRequest(request) {
        (let data: NSData?, let response: NSURLResponse?, let error: NSError?) -&gt; Void in

        // 1: Check HTTP Response for successful GET request
        guard let httpResponse = response as? NSHTTPURLResponse, receivedData = data
            else {
                print(&quot;error: not a valid http response&quot;)
                return
        }

        switch (httpResponse.statusCode)
        {
        case 200:

            let response = NSString (data: receivedData, encoding: NSUTF8StringEncoding)
            print(&quot;response is \(response)&quot;)


            do {
                let getResponse = try NSJSONSerialization.JSONObjectWithData(receivedData, options: .AllowFragments)

                EZLoadingActivity .hide()

               // }
            } catch {
                print(&quot;error serializing JSON: \(error)&quot;)
            }

            break
        case 400:

            break
        default:
            print(&quot;wallet GET request got response \(httpResponse.statusCode)&quot;)
        }
    }
    dataTask.resume()

4.33 (9 Votes)
0
Are there any code examples left?
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source