2016-02-11 116 views
1

关注此页(https://django-oauth-toolkit.readthedocs.org/en/latest/rest-framework/getting_started.html),我能够为我的django项目设置OAuth。Curl -d to Alamofire

以下curl命令为我提供了一个访问资源的标记。

curl -X POST 
-d "grant_type=password&username=<user_name>&password=<password>" 
-u"<client_id>:<client_secret>" http://localhost:8000/o/token/ 

但是,当我使用Alamofire发送请求时,事情有点奇怪。这是我的代码

Alamofire.request(.POST, url, parameters: parameters, encoding: .JSON) 
     .authenticate(user: client_ID, password: client_Secret) 

其中参数是一本字典

[ 
"password": <password>, 
"grant_type": password, 
"username": <username> 
] 

使用curl命令,我可以从Django中看到request.POST.items()返回的参数列表。但是,使用Alamofire,没有什么。参数出现在request.body中!

这个问题让我发疯。任何帮助将不胜感激!

在此先感谢。

回答

2

那么,你的curl命令发布为Content-Type: application/x-www-form-urlencoded格式,而你正在强制发布为json(.JSON)。由于这个原因,请求已经作为application/json传递给你的django,你正在看到的是参数而不是POST.items()

因此,从您的代码encoding: .JSON中删除此项。

+0

非常感谢!我昨晚想到了,现在事情进展顺利! – Yinan