0

I want to send my apiKey in headers in Alamofire to my web service.But no matter how many times I test it,I still cant to get the value of apiKey in my web service.

My web service always return this error,cause cant get the apiKey in the header.

{
  "error": true,
  "message": "Api key is missing"
}

This is how I make the request,I think I didnt do anything wrong,but the web service still cant get the value of apiKey.

    let My_URL = "My_url"
    let params : [String : Any]=["param1":value1,"param2":value2]
    let headers : HTTPHeaders = [
        "Content-Type":"application/x-www-form-urlencoded",
        "authorization" : apiKey //<--this is the value I need to 
                                      get in header of web service
    ]

    Alamofire.request(MY_URL,method: .post ,parameters : params ,headers:headers).responseJSON {
        response in
        debugPrint(response)
    }

My web service currently is serving for Android and Web version.Both running well,but I really no idea why the ApiKey in headers cant get it from webservice.

Totally no idea what is going wrong..Somebody please help!!!!

EDIT

I attach the result from debugPrint here,please take a look and let me know what is the problem,I suspect the problem is cause by the "Authorization" tag in the result below,the "Authorization" field is not the field I set,and the value as well,is not the value I want as well.

But why is this happen?? And how to solve this problem?

[Response]: <NSHTTPURLResponse: 0x60c00003d4c0> { URL: http://My_URL} { Status Code: 400, Headers {
    "Access-Control-Allow-Headers" =     (
        Authorization
    );
    "Access-Control-Allow-Methods" =     (
        "GET, POST, PUT, DELETE, OPTIONS"
    );
    "Access-Control-Allow-Origin" =     (
        "*"
    );
    Authorization =     ( // <--why this authorization appear
        16ebe49c51039ddfbb09e5df8519e755  //this is not the value I set
    );
    Connection =     (
        close
    );
    "Content-Length" =     (
        45
    );
    "Content-Type" =     (
        "application/json"
    );
    Date =     (
        "Sun, 14 Dec 2017 14:20:59 GMT"
    );
    Server =     (
        "Apach(Ubuntu)"
    );
    "X-Powered-By" =     (
        "PHP/5.5.9-1"
    );
} }

EDIT: To solve the problem above,I read this following question

iOS Alamofire Incorrect Authorization Header

How to disable the URLCache completely with Alamofire

Therefore,I modify my request as below,but the problem still the same,the result of debugPrint is still same as above.

let configuration = URLSessionConfiguration.default
configuration.urlCache = nil
let sessionManager = Alamofire.SessionManager(configuration: configuration)

let params : [String : Any]=["param1":value1,"param2":value2]
let headers : HTTPHeaders = [
    "Content-Type":"application/x-www-form-urlencoded",
    "authorization" : apiKey //<--this is the value I need to 
                                          get in header of web service
]

sessionManager.request(MY_URL,method: .post ,parameters : params ,headers:headers).responseJSON {
            response in
            debugPrint(response)
        }.session.finishTasksAndInvalidate()
ken
  • 2,426
  • 5
  • 43
  • 98
  • Maybe the API key to be sent is an empty string. Or you have the wrong header key. Instead of `authorization`, it might be `Authorization` or `API-Key` or something else. – mownier Dec 17 '17 at 14:35
  • but I print out the value if `apiKey` in the console,the value is correct,really no idea in this issue – ken Dec 17 '17 at 14:40
  • my `webservice` really looking for the key of `authorization` – ken Dec 17 '17 at 14:41
  • Can you put a breakpoint in debugPrint? Then, trace the chain of functions. – mownier Dec 17 '17 at 14:48
  • I attach the `debugPrint` result in my question already @mownier ,please take a look ya – ken Dec 17 '17 at 14:50
  • I think it is the response coming from the API server. Print the URLRequest before it is being resumed. – mownier Dec 18 '17 at 06:27

1 Answers1

0

I know this question was asked long time ago, but to solving other persons issue I answer it.

Removing Authorization header value is not a bug make by alamofire lib. This key and some other keys are NSURLSession reserved key, so according to apple document value of these keys may change. To avoid this issue you must change the header name to something like Authorization-App or any other key confirmed by your API team.

Fa.Shapouri
  • 988
  • 2
  • 12
  • 30