0

i'm testing XMLParsing library (that use Codable protocol with XML request)

XMLParsing library link : https://github.com/ShawnMoore/XMLParsing

with https://openweathermap.org API

the API link is "http://api.openweathermap.org/data/2.5/weather"

my model is

struct weather:Codable {
    let q : String
    let appid : String
    let mode : String
}

and the request is

        var request = URLRequest(url: URL(string: "http://api.openweathermap.org/data/2.5/weather")!)
        request.httpMethod = "POST"

        let post2 = weather(q: "london", appid: "f4be702b940e5073d765cb2473f0b31b", mode: "xml")


        do{

            let body = try XMLEncoder().encode(post2, withRootKey: "current")

            request.httpBody = body

        } catch{}

        let session = URLSession.shared
        let task = session.dataTask(with: request) { data, response, error in
            if error != nil {
                print("error: \(String(describing: error))")// Handle error…
                return
            }

            guard let data = data else {return }

            print("response: \(response)")
           print("data: \(data)")


    }
        task.resume()

I don't know where is the problem ! I always get error code 401

response: Optional(<NSHTTPURLResponse: 0x600003bdedc0> { URL: http://api.openweathermap.org/data/2.5/weather } { Status Code: 401, Headers {
    "Access-Control-Allow-Credentials" =     (
        true
    );
    "Access-Control-Allow-Methods" =     (
        "GET, POST"
    );
    "Access-Control-Allow-Origin" =     (
        "*"
    );
    Connection =     (
        "keep-alive"
    );
    "Content-Length" =     (
        107
    );
    "Content-Type" =     (
        "application/json; charset=utf-8"
    );
    Date =     (
        "Mon, 14 Jan 2019 07:14:16 GMT"
    );
    Server =     (
        openresty
    );
    "X-Cache-Key" =     (
        "/data/2.5/weather?"
    );
} })
data: 107 bytes

but on PostMan it working fine and get current data

enter image description here

Basel
  • 550
  • 8
  • 21
  • Check parameter is correct or not and an API key ? – Abhishek Jadhav Jan 14 '19 at 07:40
  • 1
    Your PostMan image shows that the parameters are embedded in url as query parameter, not in XML. Your Swift code is completely different that your PostMan request. – OOPer Jan 14 '19 at 07:41
  • Thank you @OOPer so I was send it in Header and it should be as Parameters – Basel Jan 14 '19 at 08:43
  • I meant Body not Header – Basel Jan 14 '19 at 09:12
  • Happy to see you could have solved your issue. By the way, (just my curiosity) why do you dare to use xml (which is not in fashion theses days and you need to use third party library to encode or decode) rather than json ? OpenWeatherMap's default output format is json. – OOPer Jan 14 '19 at 11:36
  • @OOPer same thing I told to my company , but they told me you must use XML , That why I was just testing with OpenWeatherMap =( – Basel Jan 15 '19 at 07:00

1 Answers1

0

Thanks to @OOPer

I put it as Parameters on the link and change the request to GET it working fine now

import UIKit
import XMLParsing


struct current:Codable {
    let city : City?
}

struct City:Codable {
    let id : String?

}

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()




        let request = URLRequest(url: URL(string: "http://api.openweathermap.org/data/2.5/weather?q=london&appid=f4be702b940e5073d765cb2473f0b31b&mode=xml")!)

        let session = URLSession.shared

        let task = session.dataTask(with: request) { (data, response, error) in



            do{

                guard (error == nil) else {
                    print("There is error")
                    return
                }

                guard let data = data else {return}

                let newData = try XMLDecoder().decode(current.self, from: data)

                print((newData.city?.id)!)

            }catch let error {
                print("there is error",error)
            }

            }
               task.resume()


    }


}
Basel
  • 550
  • 8
  • 21