How to successfully pass string array as parameter

2019-07-14 07:13发布

I have an endpoint that accepts a string array as parameter but I can't get it working with alamofire. I test my endpoint with postman and it works fine, even in the browser, but with alamofire it fails and just returns the whole thing (as if I didn't put any parameter).

func getQuotes(String url){

    //THIS CALL IS NOT WORKING. PARAMETERS ARE NOT SENT PROPERLY - FIX

    let stringArray : [String] = ["4250_XSAU", "Test"]
    let getQuoteParameters : Parameters = [
        //"internal_symbols": stockInternalSymbols
        "internal_symbols" : stringArray
    ]

    print("attempting to get quotes on utility queue")

    Alamofire.request(url, parameters: getQuoteParameters).responseJSON{ response in
        print(response)

       /* if (response.result.value != nil){
            let jsonResponse = JSON(response.result.value!)

           print(jsonResponse) 
        }
       */
    }
}

Am I doing something wrong? When I go to url + "?internal_symbols=["4250_XSAU","Test"] on my browser, or postman, it works just fine.

I also tried setting my "getQuoteParamaters" variable as

let getQuoteParameters : Parameters = [
        "internal_symbols" : ["4250_XSAU", "Test"]
    ]

but it doesn't work neither... it should be the same thing.

Just to clarify, this request ignores completely my parameters, when it should send the array to my backend.

3条回答
We Are One
2楼-- · 2019-07-14 07:17

You can simply pass your string array in converting it into a JSON format.Like in the sample code given below:

  var request = URLRequest(url: url)
  request.httpMethod = "POST"
  request.setValue("application/json", forHTTPHeaderField: "Content-Type")

  let values = ["06786984572365", "06644857247565", "06649998782227"]

  request.httpBody = try! JSONSerialization.data(withJSONObject: values)

 Alamofire.request(request)
.responseJSON { response in
    // do whatever you want here
    switch response.result {
    case .failure(let error):
        print(error)

        if let data = response.data, let responseString = String(data: data, encoding: .utf8) {
            print(responseString)
        }
    case .success(let responseObject):
        print(responseObject)
    }
  }
查看更多
我命由我不由天
3楼-- · 2019-07-14 07:39

My solution in Swift 3:

let text: [String] = ["location.branches.longitude", "location.branches.latitude"]

let params: Parameters = [
  "_source": text
]
查看更多
我欲成王,谁敢阻挡
4楼-- · 2019-07-14 07:40

Try by add encoding standard in your request, like JSONEncoding.default

func getQuotes(String url){

//THIS CALL IS NOT WORKING. PARAMETERS ARE NOT SENT PROPERLY - FIX

let stringArray : [String] = ["4250_XSAU", "Test"]
let getQuoteParameters : Parameters = [
    //"internal_symbols": stockInternalSymbols
    "internal_symbols" : stringArray
]

print("attempting to get quotes on utility queue")

Alamofire.request(url, method: .post, parameters: getQuoteParameters, encoding: JSONEncoding.default).responseJSON{ response in
    print(response)

   /* if (response.result.value != nil){
        let jsonResponse = JSON(response.result.value!)

       print(jsonResponse) 
    }
   */
}

Thanks.

查看更多
登录 后发表回答