I am having a problem passing a query string from swift to express. I am passing [String: Any] like so:
let params = ["id": 1]
To a function which is currently doing the following:
postString = ""
for param in params {
postString = "\(param.key)=\(content)"
if params.count > 1 && i < params.count {
postString = "&"
}
}
let url = URL(string: "http://localhost:8080/api?" postString)
guard let requestUrl = url else { fatalError() }
var request = URLRequest(url: requestUrl)
request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
let (data, response) = try await URLSession.shared.data(for: request)
So when I pass this into express I get all the parameters that Im passing in, however its showing all values as string. This is the request object:
originalUrl: '/api?id=13',
query: { id: '13' },
How can I properly pass an int through the query string? Thanks in advance for any help.
CodePudding user response:
Query parameters don't contain type information and are always strings. You will need to parseInt
on the parameter value (and handle errors appropriately).