简体   繁体   English

带有数据的Alamofire POST请求

[英]Alamofire POST Request with Data

I'm developing an App with Swift for iOS and use the Alamofire Framework for HTTP(S) requests to an API. 我正在使用iOS的Swift开发一个应用,并将Alamofire框架用于对API的HTTP(S)请求。 I already have an API "wrapper" written in PHP that works very well, but now I want to do that with Swift. 我已经有一个用PHP编写的API“包装器”,可以很好地工作,但是现在我想用Swift做到这一点。

The API needs a nonce, my API key and a signature. 该API需要一个随机数,我的API密钥和一个签名。 The signature is calculated the right way and all components together should be in the request body. 签名是按照正确的方式计算的,所有组件都应放在请求正文中。

Here the few lines of the PHP script that are working: 这是运行PHP脚本的几行:

// generating nonce and signature
$mt = explode(' ', microtime());
$req['nonce'] = $mt[1] . substr($mt[0], 2, 6);
$req['key'] = $key;
$req['signature'] = $this->get_signature($req['nonce']);

$post_data = http_build_query($req, '', '&');

static $ch = null;
if (is_null($ch))
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:52.0) Gecko/20100101 Firefox/52.0");
}

curl_setopt($ch, CURLOPT_URL, 'https://example.org/api/');
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);

This is working like a charm. 这就像一种魅力。 Alamofire should do this with even less lines of code, here's what I have: Alamofire应该用更少的代码行来做到这一点,这就是我所拥有的:

let nonce = Int((Date().timeIntervalSince1970 * 1000.0).rounded())
if let signature = self.getSignature(nonce: nonce) {
    var params = [String: Any]()
    params["nonce"] = milliseconds
    params["key"] = key
    params["signature"] = signature

    let headers = [ "Content-Type": "application/x-www-form-urlencoded", "User-Agent" : "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:52.0) Gecko/20100101 Firefox/52.0" ]
    Alamofire.request(url, method: .post, parameters: params, encoding: URLEncoding.httpBody, headers: headers).responseJSON(completionHandler: { (repsonse) in
        // ...
    })
}

But the API always returns POST Only Endpoint as plaintext. 但是,API 始终以纯文本形式返回POST Only Endpoint With PHP I get a JSON array returned. 使用PHP,我得到了一个JSON数组。

I tried to change the encoding types, added some custom header fields with different user-agents, set the Content-Type to application/x-www-form-urlencoded but nothing works. 我试图更改编码类型,使用不同的用户代理添加了一些自定义标头字段,将Content-Type设置为application/x-www-form-urlencoded但没有任何效果。

Please help me if you know some solution to this problem. 如果您知道此问题的某些解决方案,请帮助我。

OMG! 我的天啊! I found the answer to my problem. 我找到了解决我问题的方法。

The only problem was the missing / at the end of the URL. 唯一的问题是URL末尾缺少/ So eg 所以例如

https://example.org/api/endpoint1

wasn't working, but 没有工作,但是

https://example.org/api/endpoint1/

is working now. 现在正在工作。 So it absolutely has nothing to do with Alamofire itself. 因此,它绝对与Alamofire本身无关。 What a waste of time... 真是浪费时间...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM