1

I am new in swift and I want to make a post request with AlamoFire 5.4 and Swift 5

This is the object that I need to send to the server and I don't know how to create its equivalent in swift

[
  {
    "KEY": "LT_APP",
    "VALUE":"[{\"P_TIPO\":\"L\",\"P_PERNR\":\"925\",\"P_PASS\":\"GAMEROS01\",\"P_CEL\":\"6143194524\",\"P_TOKEN\":\"asdfgh\"}]"
  }
]

The content inside value is a string

In Postman looks like this

enter image description here

This is what I have

let jsonObject // Here is my problem xD how to build the object
AF.request(url,
           method: .post,parameters: jsonObject , encoding: JSONEncoding.default)
    .authenticate(username: user, password: password)
    .responseJSON { response in
        switch response.result {
            case .success(let json):
                let rtn = JSON(json)
                print(rtn["result"]["RESPONSE"][0])
            case .failure(let error):
                print(error)
        }
    }

I tried several ways to create it, inside a class, with a [String: Any] dictionary and finally the object declared directly

class Valores: NSObject{
var KEY:String
var VALUE:String

init(key: String, value: String){
    self.KEY = key
    self.VALUE = value
 }
}

var Arreglo = [Valores] = [Valores]()

let objeto : Valores = Valores(key: "LT_APP", value:"[{\"P_TIPO\":\"L\",\"P_PERNR\":\"925\",\"P_PASS\":\"GAMEROS01\",\"P_CEL\":\"6143194524\",\"P_TOKEN\":\"asdfgh\"}]")
Arreglo.append(Objeto)

Thanks

LuisOrozco
  • 13
  • 3
  • Your previous question (https://stackoverflow.com/questions/68076487/crear-un-json-en-swift-5) was closed because of lack of details/clarity. On this one, you've added "Here is my problem xD how to build the object", but you haven't shown what you've tried. How have you attempted to build the object? – jnpdx Jun 22 '21 at 04:47
  • What's your pregunta? – El Tomato Jun 22 '21 at 04:47
  • I tried various ways to create it but I am taking my first steps in swift class Valores: NSObject{ var KEY:String var VALUE:String init(key: String, value: String){ self.KEY = key self.VALUE = value } } var Arreglo = [Valores] = [Valores]() let objeto : Valores = Valores(key: "LT_APP", value:"[{\"P_TIPO\":\"L\",\"P_PERNR\":\"925\",\"P_PASS\":\"GAMEROS01\",\"P_CEL\":\"6143194524\",\"P_TOKEN\":\"asdfgh\"}]") Arreglo.append(Objeto) – LuisOrozco Jun 22 '21 at 04:53

2 Answers2

0

not sure its right way to do but may be this will work ..

    let arr = [["P_TIPO":"L"],["P_PERNR":"925"],["P_PASS":"GAMEROS01"],["P_CEL":"6143194524"],["P_TOKEN":"asdfgh"]]
    var text = "\(arr)"
    text = text.replacingOccurrences(of: "[", with: "", options: NSString.CompareOptions.literal, range:nil)
    text = text.replacingOccurrences(of: "]", with: "", options: NSString.CompareOptions.literal, range:nil)
    text = "[{\(text)}]"
    let rest = [["KEY": "LT_APP"], ["VALUE": "\(text)"]]
    print(rest)
Shivam Parmar
  • 1,520
  • 11
  • 27
  • Thanks, the object matches but when trying to send it in the request I get this error, any ideas? Cannot convert value of type '[[String : String]]' to expected argument type 'Parameters?' (aka 'Optional>') – LuisOrozco Jun 22 '21 at 05:13
  • AF.request(url, method: .post,parameters: rest, encoding: JSONEncoding.default) – LuisOrozco Jun 22 '21 at 05:13
  • remove : [[String: String]] ; `let rest = [["KEY": "LT_APP"], ["VALUE": "\(json)"]]` – Shivam Parmar Jun 22 '21 at 05:16
  • Thank you very much for solving my problem, I know that it is very simple but since I am new it has a bit of difficulty, I will continue studying a little more xD – LuisOrozco Jun 22 '21 at 05:35
  • you are alway welcome its my pleasure to help you :) – Shivam Parmar Jun 22 '21 at 05:41
0

i think you are little bit confused here. so, i explain some points.

  • First you need to pass a dictionary array in parameter as you share in image.so, declare an array of dictionary

var param = [String:Any] //here is some problem i can't input dictionary array so see in snippet.

  • after that you if you there you also put array of dictionary in parameter value [{\"P_TIPO\":\"L\",\"P_PERNR\":\"925\",\"P_PASS\":\"GAMEROS01\",\"P_CEL\":\"6143194524\",\"P_TOKEN\":\"asdfgh\"}] so, first take dictionary

let dict = ["P_TIPO":"L", "P_PERNR":"925", "P_PASS":"GAMEROS01", "P_CEL":"6143194524", "P_TOKEN":"asdfgh"]

and than put that dictionary in array

let arrVal = [dict]

  • at last set key and value of param

param = [["LT_APP":arrVal]]

  • pass param in parameters.

see the snippet code

var param = [[String:Any]]()
        
let dict = ["P_TIPO":"L", "P_PERNR":"925", "P_PASS":"GAMEROS01", "P_CEL":"6143194524", "P_TOKEN":"asdfgh"]
let arrVal = [dict]
        
param = [["LT_APP":arrVal]]
jatin fl
  • 157
  • 6