添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

UPDATE For me the Problem got fixed as soon as I was putting "encoding: URLEncoding(destination: .queryString)" in my request. Maybe this helps somebody else. link

I struggled the whole day to find the problem in my Alamofire PUT Request or the Flask Restful API. Request like GET, DELETE and POST are working fine with Alamofire , except the PUT Request. When I'm using PUT Requests in combination with Postman and Flask-Restful everything is also working fine. But as soon as I'm trying to achieve the same Result with Alamofire , I'm not getting any parameters in Flask. I tried to illustrate this in the code examples.

So in short my example illustrates the following:

DELETE Request (Same with GET and POST)

Postman: success

Alamofire: success

PUT Request

Postman: success

Alamofire: failure (parameter dictionary empty in Flask-Restful)

Here is my Python Code [API Server]:

from flask import Flask, request, jsonify
from flask_restful import Resource, Api, reqparse
app = Flask(__name__)
api = Api(app)
class Stackoverflow(Resource):
    def delete(self):
        print(request.args)
        if request.args.get('test-key') is None:
            return jsonify({"message": "failure"})
        else:
            return jsonify({"message": "success"})
    def put(self):
        print(request.args)
        if request.args.get('test-key') is None:
            return jsonify({"message": "failure"})
        else:
            return jsonify({"message": "success"})
api.add_resource(Stackoverflow, '/stackoverflow')
if __name__ == '__main__':
    app.run(debug=True, host='0.0.0.0')

If I'm using Postman, I get this result (like expected): Result in Postman

But now I'm trying to do the same with Alamofire in Swift. Same Server, nothing changed.

SWIFT demo Code [IOS APP]:

import UIKit
import Alamofire
import SwiftyJSON
class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view
        simplePUTRequest()
        simpleDELETERequest()
    func simplePUTRequest(){
        AF.request("http://localhost:5000/stackoverflow", method: .put, parameters: ["test-key":"testvalue"])
            .validate(statusCode: 200..<300)
            .responseJSON { response in
                if let data = response.data {
                    print("Result PUT Request:")
                    print(String(data: data, encoding: .utf8)!)
                    //print(utf8Text)
                }else{
    func simpleDELETERequest(){
        AF.request("http://localhost:5000/stackoverflow", method: .delete, parameters: ["test-key":"testvalue"])
            .validate(statusCode: 200..<300)
            .responseJSON { response in
                if let data = response.data {
                    print("Result DELETE Request:")
                    print(String(data: data, encoding: .utf8)!)
                    //print(utf8Text)
                }else{

Xcode Console:

Result PUT Request:
  "message": "failure"
Result DELETE Request:
  "message": "success"

python Console (both Alamofire Requests):

ImmutableMultiDict([])
127.0.0.1 - - [15/Jun/2019 21:17:31] "PUT /stackoverflow HTTP/1.1" 200 -
ImmutableMultiDict([('test-key', 'testvalue')])
127.0.0.1 - - [15/Jun/2019 21:17:31] "DELETE /stackoverflow?test-key=testvalue HTTP/1.1" 200 -

As you can see, I'm getting the success message only while using the DELETE method. Till now I tried using different encodings like URLEncoding.httpbody and URLEncoding.default, but nothing really helped. For me it seems like it's a Alamofire/Swift Problem, because in Postman the same request method is working fine. I would really appreciate your help, because I'm stuck and don't know anything further to do. I hope I didn't misunderstood something essential. Thank you in advance!

I am currently using the same version AlamoFire, and when I use the PUT method, I use it as follows:

let request = AF.request(url, method: .put, parameters: ["uid": uid],
                         encoding: JSONEncoding.default, headers: headers)
request.responseJSON(completionHandler: { response in 
    guard response.error == nil else {
        //Handle error
    if let json = response.value as? [String: Any]
    // Handle result.

The only difference to your post is that I used the encoding option. You can try to put the option and see what happens.

Thank you a lot for your answer. I'm not using JSON in the REST Api to get parameters yet, so that unfortunately doesn't work. But maybe I switch to it as a workaround, because I can't get it to work right now the other way. So your answer still helps :) – Morris Keller Jun 17, 2019 at 12:19

It looks like your server is expecting your PUT parameters to be URL form encoded into the URL. You may be hitting the version of the request method that uses JSON encoding by default, so adding encoder: URLEncodedFormParameterEncoder.default at the end of your request call should fix that. A future release will make that the default, as it's safe across all request types.

If that's not the issue, I suggest you investigate more closely to see what the differences between the requests may be. Since you control the server you should have easy access to the traffic.

Thank you for answering. It clearly makes sense to me but unfortunately still doesn't work. I'm trying to investigate more like you said. Maybe I also switch to a different way to pass the parameters when using PUT requests as a workaround till I find a solution for this problem. – Morris Keller Jun 17, 2019 at 12:24 Thank you, in the end your answer helped me to find the missing piece to fix the problem. I searched for "query string Alamofire" and found another Stackoverflow question link where somebody advised to put "encoding: URLEncoding(destination: .queryString)" in the request. No it works perfectly. Thank you for giving me a new idea :) – Morris Keller Jun 17, 2019 at 12:37

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.