ShawnMoore / XMLParsing

XMLEncoder & XMLDecoder using the codable protocol in Swift 4.2
MIT License
332 stars 100 forks source link

Error 401 with post request ! #36

Closed X901 closed 5 years ago

X901 commented 5 years ago

Hi ,

i'm testing the Library with https://openweathermap.org

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

but on PostMan it working fine

regexident commented 5 years ago

Error code 401 is coming from the web API, not this framework: https://httpstatuses.com/401

X901 commented 5 years ago

my request was wrong I was send it in body

but it should be on parameter with url link

https://stackoverflow.com/questions/54177119/get-error-401-when-post-request-using-xmlparsing-swift-library