Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error 401 with post request ! #36

Closed
X901 opened this issue Jan 14, 2019 · 2 comments
Closed

Error 401 with post request ! #36

X901 opened this issue Jan 14, 2019 · 2 comments

Comments

@X901
Copy link

X901 commented Jan 14, 2019

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
Copy link

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

@X901
Copy link
Author

X901 commented Jan 14, 2019

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

@X901 X901 closed this as completed Jan 14, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants