【问题标题】:Curl -d to Alamofire卷曲 -d 到 Alamofire
【发布时间】:2016-02-11 06:24:17
【问题描述】:

按照此页面 (https://django-oauth-toolkit.readthedocs.org/en/latest/rest-framework/getting_started.html),我能够为我的 django 项目设置 OAuth。

以下 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 中!

这个问题快把我逼疯了。任何帮助将不胜感激!

提前致谢。

【问题讨论】:

    标签: ios django swift curl alamofire


    【解决方案1】:

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

    所以从你的代码中删除它encoding: .JSON。

    【讨论】:

    • 非常感谢!昨晚我想通了,现在一切顺利!
    猜你喜欢
    • 2018-03-22
    • 2019-04-18
    • 1970-01-01
    • 2016-01-18
    • 2014-05-29
    • 2019-05-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多