0

Using Alamofire for Network call. My requirement is, need to send body in GET request. Have tried the below code

func getForms(formTypes: [Int], userInterestIds:[Int], completionHandler: @escaping (_ status: Bool, _ response : GetArticleApiResponse?, _ error: Error?) -> Void) {

let headers = [
    "Authorization" : "dfkjl23ksldjk3kd3",
    "Content-Type": "application/json"
]
var parameters: Parameters = [:]
parameters["post_types"] = formTypes
parameters["usr_intrst_ids"] = userInterestIds

Alamofire.request(finalUrl, method: .get, parameters: parameters, headers: headers).responseJSON { response in
    switch response.result {
    case .success:
        completionHandler(true,responseData, nil)
    case .failure(let error):
        completionHandler(false,nil,error)
    }
}

But, didn't get expected response. If i'm doing wrong. Help me, how to implement the below postman call.

enter image description here

Thanks in advance.

Ramdhas
  • 1,765
  • 1
  • 18
  • 26
  • Doesn't look like you're using Params in Postman. – Don Jun 27 '19 at 18:15
  • You are showing some API `.../forms` API with GET request in image while trying filter API in code. filter API takes parameters as well it should be POST request. – Ankit Jayaswal Jun 27 '19 at 18:31
  • @Don Thanks for the response. Ok, my requirement is need to send body in GET call. How to do. Now, i'm updated my question. – Ramdhas Jun 28 '19 at 05:41
  • @AnkitJayaswal Thanks for response. Yes, even i asked the to make it POST call, but backend developer says, this should be in GET call. Finally, my doubt is, is it possible using alamofire to make this call successful. Pls check the postman image – Ramdhas Jun 28 '19 at 05:45
  • try to send parameters like this var parameters: Parameters = [ "post_types": formTypes, "usr_intrst_ids": userInterestId ] – channu Jun 28 '19 at 05:45
  • Not working as expect. Server not able to read the body from request. – Ramdhas Jun 28 '19 at 06:04
  • try adding encoding: JSONEncoding.default in alamofire request – Samir Shaikh Jun 28 '19 at 07:11
  • Tried not working. Tried custom encoding too. No luck – Ramdhas Jun 28 '19 at 10:19

1 Answers1

0

Personally, I'd use a Struct that conforms to the Codable protocol.

struct MyStruct: Codable {
  var post_types: [Int]
  var usr_intrst_ids: [Int]
}

func getForms(formTypes: [Int], userInterestIds:[Int], completionHandler: @escaping (_ status: Bool, _ response : GetArticleApiResponse?, _ error: Error?) -> Void) {

  let myStruct = MyStruct(post_types: formTypes, usr_intrst_ids: userInterestIds)

  do {
    json = try JSONEncoder().encode(myStruct)
    var request = URLRequest(url: finalUrl)
    request.httpMethod = HTTPMethod.get.rawValue
    request.setValue("application/json; charset=UTF-8", forHTTPHeaderField: "Content-Type")
    request.setValue("dfkjl23ksldjk3kd3", forHTTPHeaderField: "Authorization")
    request.httpBody = json

    Alamofire.request(request).responseJSON { response in
      ...
    }
  } catch { // unable to encode myStruct }
Don
  • 479
  • 1
  • 4
  • 7