Jazzin
Jazzin

Reputation: 55

Getting 200 response, but not showing json value

enter image description hereI have wrote a networking request using the "wordsapi", and I am getting a 200 status, but the response I am receiving is not in json.

My code builds, and runs I just need the response in json.

    let wordsAPI = 
    "https://wordsapiv1.p.mashape.com/words/money/synonyms"
    let wordsAPIEndPoint = NSURL(string: wordsAPI)
    let request = NSMutableURLRequest(url: wordsAPIEndPoint! as 
     URL)

    request.setValue("mapkey", forHTTPHeaderField: "X-Mashape-Key")
    request.httpMethod = "GET"
    let session = URLSession.shared

    let synonymResponse = session.dataTask(with: request as 
     URLRequest) { (data, response, error) -> Void in
        if let resp = response as? HTTPURLResponse {
           print(resp)
        }
     }
     synonymResponse.resume()

//response I am receiving

    { Status Code: 200, Headers {
    "Accept-Ranges" =     (
    bytes
    );
    "CF-RAY" =     (
     "4d4d9aacabdccf5c-IAD"
    );
    Connection =     (
    "keep-alive"
    );
    "Content-Length" =     (
    30
    );
    "Content-Type" =     (
    "application/json; charset=utf-8"
     );Date =     (
    "Fri, 10 May 2019 17:24:00 GMT"
      );

I need my response to be in json for the synonyms of whatever value I set as my endpoint. In this case it is "money"

Upvotes: 0

Views: 1581

Answers (1)

Shehata Gamal
Shehata Gamal

Reputation: 100503

You need to use the data var in completion that contains the received json data

let wordsAPI = "https://wordsapiv1.p.mashape.com/words/money/synonyms"
let wordsAPIEndPoint = URL(string: wordsAPI)
var request = URLRequest(url: wordsAPIEndPoint!) 
request.setValue("mapkey", forHTTPHeaderField: "X-Mashape-Key")
request.httpMethod = "GET"

URLSession.shared.dataTask(with: request) { (data, response, error) -> Void in
    guard let data = data else { return }
    let res = try! JSONDecoder().decode(Root.self,from:data)
    print(res.synonyms)
}.resume()

struct Root:Codable {
  let word:String
  let synonyms:[String]
}

Upvotes: 2

Related Questions