1

my company and I are working on a iOS Project, and we get stacked in this issue. We are trying to make a POST request to our API, but all we are getting is a success code but with a reponse of type:

SUCCESS: { detail = "Authentication credentials were not provided."; }

Here is our code:

@IBAction func accept(_ sender: Any) {
    var comm,cat,dist : String!
    comm = observationstwee.text!
    print(comm)
    let catd = 7
    cat = String(catd)
    print(cat)
    dist = distancetwee.text!
    print(dist)
    let parameters: Parameters = [
        "comment":comm,
        "category":cat,
        "distance":dist,
        "timestamp":time
    ]
    let headers: HTTPHeaders = ["Authorization": "Token \(token!)"]
    print(headers.debugDescription)
    Alamofire.request("https://llegoelbigotes.ubiqme.es/api/new-travel",parameters: parameters, headers: headers).responseJSON {response in switch(response.result) {

    case .success(_):
        print(response)
        print("Correct Travel")
        //changeViewController(storyboard_name: "Main", viewcontroller_name: "tabBarController", context: self)
        break

    case .failure(_):
        print("FAILURE")
        }
    }
}

Thanks for the help we are working with Alamofire 4.5 and iOS 11.2

Pol Valls Ortiz
  • 282
  • 2
  • 14
  • check the name of the autorization key – Reinier Melian Jan 31 '18 at 15:45
  • 1
    check your token has a value and check how your endpoint handles authorisation. try using the postman google extension to send the request via that and see if it works. if it doesnt then the issue is with the API rather than your code – Scriptable Jan 31 '18 at 15:45
  • maybe you need to use different scheme, based on how your api layer implemented, please check this post - https://stackoverflow.com/questions/33265812/best-http-authorization-header-type-for-jwt. Maybe try using Bearer vs Token – Vlad Z. Jan 31 '18 at 15:52

2 Answers2

1

I guess this will work.

let url = "https://llegoelbigotes.ubiqme.es/api/new-travel"

Alamofire.request(
        url,
        method: .post,
        parameters: parameters,
        encoding: JSONEncoding.default,
        headers: headers). validate().responseJSON{ response in 
        switch(response.result) {

        case .success(_):
            print(response.response)
            print("Correct Travel")
           //changeViewController(storyboard_name: "Main", viewcontroller_name: "tabBarController", context: self)
           break

        case .failure(_):
        print("FAILURE")
     }
 }

If not then try to check your headers variable of type HTTPHeaders

let headers: HTTPHeaders = ["Authorization": "Token \(token!)"]

As here you are appending Token with your token string. say your token is XXXX then it becomes TokenXXXX. Try using it like

let headers: HTTPHeaders = ["Authorization": " \(token!)"]

Then Check.

Yaseen Majeed
  • 350
  • 2
  • 11
  • Not workin, getting Alamofire.AFError.responseValidationFailed(Alamofire.AFError.ResponseValidationFailureReason.unacceptableStatusCode(403)) @Yaseen – Pol Valls Ortiz Jan 31 '18 at 19:46
  • The 403 Forbidden error is an HTTP status code that means that accessing the page or resource you were trying to reach is absolutely forbidden for some reason. Try printing out everything before making the POST call you might have issues with your Token. I suggest, try doing the POST call via POSTMAN or [hurl](https://www.hurl.it). – Yaseen Majeed Feb 01 '18 at 05:30
0

1) You are sending GET request. I don't see in Alamofire call specified another method and, AFAIK, default is GET.

2) Chain validate() method before responseJSON so your request call will look like this:

Alamofire.request("https://llegoelbigotes.ubiqme.es/api/new-travel", method: .post, parameters: parameters, headers: headers).validate().responseJSON { ...

Another possible fix to this problem is using another encoding. In your call to alamofire set encoding: URLEncoding(destination: .httpBody)

Check this out for more information.

Maksym Musiienko
  • 1,248
  • 8
  • 16