0

I've built an iOS app that hits my custom Django server. I am using AlamoFire to make the HTTP requests. The following is my code in Swift that I got from here (note that url is a variable I created beforehand with the correct path).

    let headers: HTTPHeaders = ["Authorization" : accessToken, "Content-Type" : "application/json"]
    AF.request(url,
               method: .get,
               parameters: params,
               encoding: URLEncoding.default,
               headers: headers,
               interceptor: nil).validate().responseJSON { response in
        switch response.result {
        case .success:
            if let json = response.value as? [String : AnyObject] {
                completion?(json, nil)
            }
        case .failure(let error):
            completion?(nil, error)
        }
    }

However, when I try to get my authorization token on my Django server like so (which I found from here):

request.META.get("HTTP_AUTHORIZATION")

I get None as the return type. I'm rather new to using Alamofire and also Django so I am unsure as to where the problem lies. I've tried looking up solutions for both the iOS and the Django side, but to no avail.

As a side note, I used to work on a project using Flask and was able to get the Authorization header like so:

request.headers.get('Authorization')
kbunarjo
  • 1,277
  • 2
  • 11
  • 27
  • To clarify the question: 1. In your swift part, do you get correct response result? 2. Did you try to use e.g. Postman app to test your back-end? – Aleksey Potapov Nov 27 '19 at 06:43
  • This question covers two areas: back-end issues, that influences frond-end as the result. If you have issue on your server I would recommend to create a new question with Python, django tags and provide more details on that, so proper community could help you. The when you are done switch to front-end part. – Aleksey Potapov Nov 27 '19 at 06:45
  • 1. I do not get the correct response. My server is unable to handle the request because there is no authentication token. 2. Let me look into using Postman! – kbunarjo Nov 27 '19 at 06:45

1 Answers1

0

try:

let header : HTTPHeaders = ["Authorization": "Token (djangotoken)" ,"Content-type" : "application/json"]

So put the word Token before the djangoToken.

saro
  • 705
  • 3
  • 13