HttpRequest with multiple parameters swift - swift

I'm trying to create a request with multiple parameters using Swift. So far I managed to create with one parameter but not with multiple.
I tried to use a Dictionary but couldn't do it.
Here is my actual code:
let protocolo = txtProtocolo.text!
var request = URLRequest(url: url)
let parameters = "protocolo=\(protocolo) "
request.httpMethod = "POST"
request.httpBody = parameters.data(using: String.Encoding.utf8)
URLSession.shared.dataTask(with: request)
{ (data, response, error) in
....
I'm trying to do something like this:
let dictionary = ["protocolo":protocolo,
"secondParameter": "value"]
And use this dictionary as httpBody.
Thanks in advance for your help.

If you have the option use Alamofire. It is very good :)
But if you want to use the dictionary. It seems you have to convert it to a string. Did you try something like
let parameters = ["auth":"asdf", "width":"123"]
let parametersString = (parameters.compactMap({ (key, value) -> String in
return "\(key)=\(value)"
}) as Array).joined(separator: "&")
And use the parametersString as the parameter

Related

Simple post request in SwiftUI

I'm beginner in SwiftUI and I'm not familiar with variable management.
I'd like to send a very simple post request like this one with SwiftUI:
let full_path : String = "https://www.example.com/get_answer?my_message=current temperature"
I've tried with this piece of code but it didn't work.
if (URL(string: full_path) != nil) {
let url = URL(string: full_path)!
var request = URLRequest(url: url)
request.httpMethod = "POST"
var decodedAnswer = String("")
do {
let task = URLSession.shared.dataTask(with: url) { data, response, error in
print(response)
decodedAnswer = String(decoding: response, as: UTF8.self)
}
}
I have the following error:
Value of optional type 'URLResponse?' must be unwrapped to a value of
type 'URLResponse'
I don't know how to get the response.
How can I get the response from a simple Post request in SwiftUI?
Multiple issues here.
You are trying to decode the URLResponse object, but what you want is the data object in the decoder.
You seem to not know about optionals. I would refer you to the basic Apple tutorials about this topic. You can find it with your favorite search engine.
You are in an async context here. Everything inside the url datasession closure will be execute after your network request returns. The code in your function will be completed by that moment and your var decodedAnswer will be out of scope. So move it out of the function in to the class/struct.
You probably want something like this:
This should be defined in class scope or you won´t be able to use it:
var decodedAnswer: String = ""
This should be in a function:
let full_path: String = "https://www.example.com/get_answer?my_message=current temperature"
if let url = URL(string: full_path) {
var request = URLRequest(url: url)
request.httpMethod = "POST"
do {
let task = URLSession.shared.dataTask(with: url) { data, response, error in
//This converts the optionals in to non optionals that could be used further on
//Be aware this will just return when something goes wrong
guard let data = data, let response = response, error == nil else{
print("Something went wrong: error: \(error?.localizedDescription ?? "unkown error")")
return
}
print(response)
decodedAnswer = String(decoding: data, as: UTF8.self)
}
task.resume()
}
}

How can I pass parameters in an HTTP Post request in Swift?

Am working on a simple Swift test app which just calls Perl script on my server. Right now I just want to send over a username and id, and get them back in a JSON response. Nothing more, am still in the learning stage.
But no matter which way I try, I cannot successfully send the two parameters in my URLRequest.
In the sample below, you'll see I try to send them in the main url, I've tried to add them as forHTTPHeaderFields, but the response I get back in my URLSessionDataDelegate is always:
data is {"userid":"","username":""}
JSON Optional({
userid = "";
username = "";
let file = File(link: "http://example.com/cgi-bin/swift.pl?username=John&userid=01", data: "hello")
uploadService.start(file: file)
And within my instance of URLSession I have tried:
// From one of my view controllers I create a File struct
// from a YouTube lesson. Eventually I want to send a file.
// So for now am using just *Hello*:
let uploadTask = UploadTask(file: file)
let url = URL(string: file.link)!
let uploadData = Data(file.data.utf8)
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.setValue("application/json", forHTTPHeaderField:"Content-Type")
request.addValue("application/json", forHTTPHeaderField: "Accept")
request.addValue("John", forHTTPHeaderField: "username")
request.addValue("01", forHTTPHeaderField: "userid")
uploadTask.task = uploadSession.uploadTask(with: request, from: uploadData)
uploadTask.task?.resume()
Every other part of the Swift test works, I get a response and data in my URSessionDelegate, and no errors. Obviously I just can't figure out how to properly send over the two parameters. For the record:
the Perl script below does work from a linux command line, or when called from a web browser.
If I hardcode the return repsonse in the perl script below, I do recieve it in the my URLSessionDelegate, so I know that I am parsing it correctly
As well, my server's error log shows that $header1 and $header2 never get initialized.
#!/usr/bin/perl -w
use strict;
use CGI;
use JSON;
my $q = new CGI;
my $header1 = $q->param("username");
my $header2 = $q->param("userid");
print $q->header('application/json');
my %out = (username=>"$header1", userid=>"$header2");
my $json = encode_json \%out;
print $json;
exit(0);
You are sending the parameters username and userid as http header values.
Your perl scrip is expecting them a query parameters.
So first create a URLComponents object, than add query items and finally create your url.
Try this:
let uploadTask = UploadTask(file: file)
var urlComponents = URLComponents(string: file.link)!
let queryItems = [URLQueryItem(name: "username", value: "John"),
URLQueryItem(name: "userid", value: "01")]
urlComponents.queryItems = queryItems
let url = urlComponents.url!
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.setValue("application/json", forHTTPHeaderField:"Content-Type")
request.addValue("application/json", forHTTPHeaderField: "Accept")
uploadTask.task = uploadSession.uploadTask(with: request, from:
uploadData)
uploadTask.task?.resume()
Have a look at this Post that shows how to add query parameters using an extension to URL
In these two lines:
request.addValue("John", forHTTPHeaderField: "username")
request.addValue("01", forHTTPHeaderField: "userid")
You are adding those as http headers and not url query parameters.
To add query parameters, you need to convert to URLComponents first and then convert back: https://developer.apple.com/documentation/foundation/urlcomponents
var urlComponents = URLComponents(string: file.link)!
urlComponents.queryItems = [
URLQueryItem(name: "username", value: "name"),
URLQueryItem(name: "userid", value: "id")
]
let newURL = urlComponents.url!
//use the newURL
Just create a dictionary with data
let parameterDictionary = ["username" : "John", "userid": "01"]
Then create httpBody object using
guard let httpBody = try? JSONSerialization.data(withJSONObject: parameterDictionary, options: []) else { return }
Then simply add that body in your request parameter
request.httpBody = httpBody
I finally found the answer here on StackOverflow.
Having no experience in http methods, the short answer to my question is that if I am using "GET", I would use urlComponents.queryItems, but if I am using "POST" then my parameters would have to be in the http body itself.
But more importantly, the answer found in the link explains when and why you should use "GET" as opposed to "POST", and vice-versa.
So to anyone coming across this, definitely read the answer provided in the link.

How to upload data using the new swift async/await methods?

The new features in Swift with async/await allow a better control about the process and a simplification in coding. But I cannot find out how this method can be applied for requests which go above a simple data reading. E.g. I need to pass a parameter in order to get a specific answer from a SQL database in the backend (accessed via php).
At first my code about the "standard" way to start with. This function reads all customer records and stores them into an account-array:
#available(iOS 15.0, *)
static func getCustomerListFromBackend() async throws -> [Account] {
let url = URL(string: "https://xxx.de/xxx/getCustomerList.php")!
let (data, _) = try await URLSession.shared.data(from: url)
var accounts: [Account] = []
accounts = try JSONDecoder().decode([Account].self, from: data)
return accounts
}
In order to make my question clear, now a piece of code in which the central statement does not work and exist. In this function I want to check whether a customer exists in the database and I need to pass the emailAddress as a parameter.
#available(iOS 15.0, *)
static func checkCustomerExistsInBackend(emailAddress: String) async throws -> String {
let url = URL(string: "https://xxx.de/xxx/checkCustomerexists.php")!
var request = URLRequest(url: url)
request.httpMethod = "POST"
var dataString = "EmailAddress=\(emailAddress)"
let dataD = dataString.data(using: .utf8)
// Statement which does not work but for which I need an alternative
let (data, _) = try await URLSession.shared.upload(request: request, data: dataD)
let answer = try JSONDecoder().decode(BackendMessage.self, from: data)
return answer.Message
}
Unfortunately there is no statement for URLSession.shared.upload(request: request, data: dataD) Until now (before async/await), I used URLSession.shared.uploadTask(with: request, from: dataD) and then used .resume() to process it. This method however gave me too many problems in controlling the right sequence of tasks in the app. Async/await could simplify this very much as in my first example.
So, is there a way to realize this? Any advice would be appreciated.
you could try using URLComponents something like:
func checkCustomerExistsInBackend(emailAddress: String) async throws -> String {
if let url = URL(string: "https://xxx.de/xxx/checkCustomerexists.php"),
var components = URLComponents(url: url, resolvingAgainstBaseURL: false) {
components.queryItems = [URLQueryItem(name: "EmailAddress", value: emailAddress)]
var request = URLRequest(url: components.url!)
request.httpMethod = "POST"
let (data, _) = try await URLSession.shared.data(for: request)
let answer = try JSONDecoder().decode(BackendMessage.self, from: data)
return answer.Message
}
throw URLError(.badURL)
}
My question was answered by Florian Friedrich's comment and workingdog's answer as well. To the later one I had to make a little adoption which I want to reflect here in this wrap up in case it can be helpful for someone with a similar problem. I show here 2 solutions to my problem with a few remarks.
Applying Florian's answer.
This was straightforward and worked right away:
static func checkCustomerExistsInBackend(emailAddress: String) async throws -> String {
let url = URL(string: "https://xxx.de/xxx/checkCustomerexists.php")!
var request = URLRequest(url: url)
request.httpMethod = "POST"
let dataString = "EmailAddress=\(emailAddress)"
let dataD = dataString.data(using: .utf8)
let (data, _) = try await URLSession.shared.upload(for: request, from: dataD!, delegate: nil)
let answer = try JSONDecoder().decode(BackendMessage.self, from: data)
return answer.Message
}
The proposal from workingdog:
Here I noticed that although the url appeared to be correctly set (ending with checkCustomerexists.php?EmailAddress=test#gmx.de), the parameter did not arrive in my php object. After some tests I found out that it works when I use GET instead of POST. So in my php file I changed the line $EmailAddress = $_POST[EmailAddress]; to $EmailAddress = $_GET['EmailAddress'];. (I am sure there is a good reason for this and I am just not experienced enough to recognize this.) Accordingly the code I use for workingdog's proposal is slightly adjusted:
func checkCustomerExistsInBackend3(emailAddress: String) async throws -> String {
if let url = URL(string: "https://xxx.de/xxx/checkCustomerexists.php"),
var components = URLComponents(url: url, resolvingAgainstBaseURL: false) {
components.queryItems = [URLQueryItem(name: "EmailAddress", value: emailAddress)]
var request = URLRequest(url: components.url!)
request.httpMethod = "GET"
let (data, _) = try await URLSession.shared.data(for: request)
let answer = try JSONDecoder().decode(BackendMessage.self, from: data)
return answer.Message
}
throw URLError(.badURL)
}

Pass array in HTTP request body

I want to pass array like this in my request:
{
"ids": ["5ed7603ab05efe0004286d19", "5ed7608ab05efe0004286d1a"]
}
I did like that but I am not sure the server is getting as I intended:
request.httpBody = try JSONSerialization.data(withJSONObject: ids, options: .prettyPrinted)
You should be using a Dictionary for this purpose. Here's how:
let dictionary = ["ids": ["5ed7603ab05efe0004286d19", "5ed7608ab05efe0004286d1a"]]
request.httpBody = try JSONSerialization.data(withJSONObject: dictionary, options: .prettyPrinted)
Suggestion: The modern approach would be to use JSONEncoder(). You could google this, there are plenty of solutions online. If you're still struggling you can ask for the method in the comments, I'll help you figure out.
Update: How you can implement Swift's JSONEncoder API in your code.
let dictionary = ["ids": ["5ed7603ab05efe0004286d19", "5ed7608ab05efe0004286d1a"]]
request.httpBody = try JSONEncoder().encode(dictionary)
Using a struct would be much safer. Here's how:
typealias ID = String
struct MyRequestModel: Codable { var ids: [ID] }
let myRequestModel = MyRequestModel(ids: ["5ed7603ab05efe0004286d19", "5ed7608ab05efe0004286d1a"])
request.httpBody = try JSONEncoder().encode(myRequestModel)
Note: Usage of type-alias is optional it just adds a bit of readability to your code, as is the usage of JSONDecoder.
You can encode using JSONEncoder;
let yourList = ["5ed7603ab05efe0004286d19", "5ed7608ab05efe0004286d1a"]
struct DataModel: Encodable {
var ids: [String]
}
let data = DataModel(ids: yourList)
let encodedData = try! JSONEncoder().encode(data)
// JSON string value
let jsonString = String(data: encodedData, encoding: .utf8)

Alamofire Custom Parameter Encoding

I'm using Alamofire and want to encode my parameters with content type "text/html; charset=utf-8". I followed the documentation https://github.com/Alamofire/Alamofire for custom parameter encoding and created
let custom: (URLRequestConvertible, parameters: [String: AnyObject]?) -> (NSURLRequest, NSError?) = {
(URLRequest, parameters) in
let mutableURLRequest = URLRequest.URLRequest.mutableCopy() as! NSMutableURLRequest
mutableURLRequest.setValue("text/html; charset=utf-8", forHTTPHeaderField: "Content-Type")
mutableURLRequest.body = // don't know if I need to set this
return (mutableURLRequest, nil)
}
func postData(){
Alamofire.request(.POST, baseUrl, parameters: parameters, encoding: .Custom(custom))
.responseString{ (request, response, data, error) in
println("blah")
}
}
I have a problem when I try to use custom in my Alamofire request and get the error "Cannot make responseString with argument list of type ( _, _, _, _)-> _ )" However, this isn't a problem if the encoding is changed to a preset like .URL so the issue seems to be in my implementation of custom?
If it makes a difference my parameters are set here:
var parameters = [String: AnyObject]()
func setParams(){
parameters = [
"CONTRACT ID" : chosenContract!.iD.toInt()!,
"FEE AMOUNT" : 0,
"TRANSACT DATE" : today
]
}
You have a couple questions in here. Let's break them down 1x1.
Compiler Issue
Your compiler issue is due to the fact that your return tuple is the wrong type. In Alamofire 1.3.0, we changed the return type to be an NSMutableURLRequest which ends up making things much easier overall. That should fix your compiler issue.
Setting the HTTPBody
Now you have a couple options here.
Option 1 - Encode Data as JSON
let options = NSJSONWritingOptions()
let data = try NSJSONSerialization.dataWithJSONObject(parameters!, options: options)
mutableURLRequest.setValue("application/json", forHTTPHeaderField: "Content-Type")
mutableURLRequest.HTTPBody = data
From what you posted I'm assuming you actually want .URL encoding for the parameters.
Option 2 - Use the .URL Case
let parameters: [String: AnyObject] = [:] // fill in
let encodableURLRequest = NSURLRequest(URL: URL)
let encodedURLRequest = ParameterEncoding.URL.encode(encodableURLRequest, parameters).0
let mutableURLRequest = NSMutableURLRequest(URL: encodedURLRequest.URLString)
mutableURLRequest.HTTPMethod = "POST"
mutableURLRequest.setValue("text/html; charset=utf-8", forHTTPHeaderField: "Content-Type")
Alamofire.request(mutableURLRequest)
.response { request, response, data, error in
print(request)
print(response)
print(error)
}
Hopefully that helps get you going. Best of luck!