Reputation: 13
I am trying to replicate following GET request in my project:
curl -XGET 'https://api2.branch.io/v1/url?url=https://example.app.link/WgiqvsepqF&branch_key=key_live_kaFuWw8WvY7yn1d9yYiP8gokwqjV0Swt'
Here is my final code:
if let url = URL(string: "https://example.app.link/WgiqvsepqF&branch_key=key_live_kaFuWw8WvY7yn1d9yYiP8gokwqjV0Swt"){
let urlRequest = URLRequest(url: url)
URLSession.shared.dataTask(with: urlRequest) { (data, response, error) in
if error == nil{
do{
if let dataReceived = data, let jsonData = try JSONSerialization.jsonObject(with: dataReceived, options: .mutableLeaves) as? [String : Any]{
print(jsonData)
}
} catch let error{
print(error.localizedDescription)
}
}
}.resume()
}
I am getting following error:
The data couldn’t be read because it isn’t in the correct format.
I tried other Stackoverflow and Reddit solution but nothing seems to be working.
Upvotes: 1
Views: 198
Reputation: 809
API call is returning data keys in wrong format. You are receiving the keys starting from '$' or '~' e.g. '$og_title', '~stage'. That's why it's showing you an error.
This is your API Call
let url = URL(string: "https://api2.branch.io/v1/url?url=https://example.app.link/WgiqvsepqF&branch_key=key_live_kaFuWw8WvY7yn1d9yYiP8gokwqjV0Swt")
let request = NSMutableURLRequest(url: url!)
request.httpMethod = "GET"
let session = URLSession.shared
let task = session.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) in
guard ((data) != nil), let _: URLResponse = response, error == nil else {
print("error")
return
}
if let dataString = NSString(data: data!, encoding: String.Encoding.utf8.rawValue) {
print(dataString)
do {
//here dataResponse received from a network request
let decoder = JSONDecoder()
let model = try decoder.decode(ResponseData.self, from: data!) //Decode JSON Response Data
// Access your json data from Swift Structs e.g. model.type
print(model.data?.iurl)
} catch let parsingError {
print("Error", parsingError)
}
}
})
task.resume()
This is your Model Struct
struct ResponseData : Codable {
let data : MyData?
let type : Int?
let tags : [String]?
let campaign : String?
let feature : String?
let channel : String?
let stage : String?
enum CodingKeys: String, CodingKey {
case data = "data"
case type = "type"
case tags = "tags"
case campaign = "campaign"
case feature = "feature"
case channel = "channel"
case stage = "stage"
}
}
struct MyData : Codable {
let custom_array : [Int]?
let log_title : String?
let custom_boolean : Bool?
let custom_integer : Int?
let icreation_source : Int?
let log_description : String?
let log_image_url : String?
let istage : String?
let custom_string : String?
let ifeature : String?
let url : String?
let custom_object : Custom_object?
let iurl : String?
let ldesktop_url : String?
let itags : [String]?
let lcanonical_identifier : String?
let lone_time_use : Bool?
let iid : String?
let icampaign : String?
let ichannel : String?
enum CodingKeys: String, CodingKey {
case custom_array = "custom_array"
case log_title = "$og_title"
case custom_boolean = "custom_boolean"
case custom_integer = "custom_integer"
case icreation_source = "~creation_source"
case log_description = "$og_description"
case log_image_url = "$og_image_url"
case istage = "~stage"
case custom_string = "custom_string"
case ifeature = "~feature"
case url = "url"
case custom_object = "custom_object"
case iurl = "+url"
case ldesktop_url = "$desktop_url"
case itags = "~tags"
case lcanonical_identifier = "$canonical_identifier"
case lone_time_use = "$one_time_use"
case iid = "~id"
case icampaign = "~campaign"
case ichannel = "~channel"
}
}
struct Custom_object : Codable {
let random : String?
enum CodingKeys: String, CodingKey {
case random = "random"
}
}
Upvotes: 1
Reputation: 1436
The decoding code is correct.
The link is wrong, it should be https://api2.branch.io/v1/url?url=https://example.app.link/WgiqvsepqF&branch_key=key_live_kaFuWw8WvY7yn1d9yYiP8gokwqjV0Swt
and not https://example.app.link/WgiqvsepqF&branch_key=key_live_kaFuWw8WvY7yn1d9yYiP8gokwqjV0Swt
Only decoding:
import UIKit
var json = """
{
"data": {
"custom_array": [
1,
2,
3,
4,
5,
6
],
"$og_title": "Title from Deep Link",
"custom_boolean": true,
"custom_integer": 1243,
"~creation_source": 0,
"$og_description": "Description from Deep Link",
"$og_image_url": "http://www.lorempixel.com/400/400/",
"~stage": "new user",
"custom_string": "everything",
"~feature": "onboarding",
"url": "https://example.app.link/WgiqvsepqF",
"custom_object": {
"random": "dictionary"
},
"+url": "https://example.app.link/WgiqvsepqF",
"$desktop_url": "http://www.example.com",
"~tags": [
"one",
"two",
"three"
],
"$canonical_identifier": "content/123",
"$one_time_use": false,
"~id": "423196192848102356",
"~campaign": "new product",
"~channel": "facebook"
},
"type": 0,
"tags": [
"one",
"two",
"three"
],
"campaign": "new product",
"feature": "onboarding",
"channel": "facebook",
"stage": "new user"
}
"""
let data = json.data(using: .utf8)!
do {
if let jsonData = try JSONSerialization.jsonObject(with: data, options: .mutableLeaves) as? [String : Any] {
print(jsonData)
}
} catch let error {
print(error)
}
Upvotes: 0