How to pass a CPF as a parameter of the Nsdictionary type for a call with JSON and POST?

Asked

Viewed 92 times

0

Hello, I have a question. I need to pass a CPF entered by the user as a parameter in my POST call to return with his data. The call works with a CPF already registered, but only works if the CPF is passed directly as parameter, as I did below and type NSDictionary. How to pass the CPF entered by the user as a parameter in the call? Follow the part of my code where I call:

let mutableURLRequest = NSMutableURLRequest(URL: URL)

mutableURLRequest.HTTPMethod = "POST"

let cpfPesquisa = cpf

let parameters: NSDictionary = ["cpf":"098.748.876-32"]

do {
    mutableURLRequest.HTTPBody = try NSJSONSerialization.dataWithJSONObject(parameters, options: NSJSONWritingOptions())
    mutableURLRequest.addValue("application/json", forHTTPHeaderField: "Content-Type")
    mutableURLRequest.addValue("application/json", forHTTPHeaderField: "Accept")

The constant parameters is passed to take the data of this CPF, but I want that in place of this predefined CPF is the one typed by the user (cpfPesquisa), how can I do that? Thanks for your help.

1 answer

2


Try to change that:

let parameters: NSDictionary = ["cpf":"098.748.876-32"]

That’s why:

let parameters: NSDictionary = ["cpf":cpfPesquisa]

Or, as suggested by iTSangar in a comment, so:

let parameters: ["cpf":cpfPesquisa]
  • 1

    recalling that the NSDictionary may be omitted from the declaration: let parameters = ["cpf":cpfPesquisa]

  • @iTSangar Thanks for the reminder.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.