Alamofire POST授权

时间:2015-08-04 20:35:26

标签: ios swift alamofire

我在ios中使用swift一个请求POST时出现问题...

在卷曲中,我发送一个这样的请求:

curl -H "Authorization: token ce2800d53d520b4a73a005a611d53d299e0c1d5e"....

和我的服务器响应200

但是我想我这样请求:

let parameters = [
    "date_of_birth": strDate!,
    "address": strPlace!,
]

let parametersH = [
    "Authorization": "token 9640e65f66429415fb9359739ed8bc3f57cb0566"
]


Alamofire.request(Alamofire.Method.POST, "http://192.168.1.70:8000/profileMe/", parameters: parameters,headers:parametersH).progress { bytesWritten, totalBytesWritten, totalBytesExpectedToWrite in
    }.responseJSON{ request, response, JSON, error in

        if (error == nil) {
            println(response)
        }
        else{
            println(error)
        }

    }

我服务器发送一个403 ....

如何通过一次有效身份验证让我请求?....

我不知道......因为标题不正确...当我发送请求时...

当我发送此请求时,我没有问题.....并且请求类似......

2 个答案:

答案 0 :(得分:1)

我遇到了同样的问题并且已经调试了一整天并最终弄明白了。我几乎肯定OP正在使用Django。

它与Alamofire或底层的Swift / Objective C网络代码相比具有微妙之处。我认为如果Alamofire在响应头中收到Set-Cookie,它将在后续请求头中将此信息作为Cookie有效负载发送。这是与其他HTTP网络库不同的行为。

由于它与Django有关,Alamofire会在后续请求中发送sessionid cookie值,这使得Django认为会话已经启动,然后在使用SessionAuthentication时它还没有提供正确的CSRF令牌。 #39; ll抛出403权限错误。这就是你第一次成功执行请求然后第二次抛出403的原因。

要解决此问题,并且通常要阻止Alamofire自动发送Cookie值,请设置

let headers = [
    "Cookie": ""
]

答案 1 :(得分:0)

我认为Authorization标头区分大小写。 所以,令牌应该是令牌。

GLOBAL $wpdb;
$wpdb->get_results($wpdb->prepare("SELECT * FROM `ninja_table` WHERE `date_modified` = %s", $strDate));