Swift: Adding Headers to my REST POST Request - ios

I am still learning Swift and I am trying to make a POST request to my web service via my new iOS App written in Swift.
I need to know how to add 2 headers to my already existing code. Also am I adding the parameters correctly?
What I have so far:
let myUrl = NSURL(string: "https://MY-MOBILE-SERVICE.azure-mobile.net/api/login");
let request = NSMutableURLRequest(URL:myUrl!);
request.HTTPMethod = "POST";
// Compose a query string
let postString = "email=myemail#website.com&password=123";
request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding);
let task = NSURLSession.sharedSession().dataTaskWithRequest(request) {
data, response, error in
if error != nil
{
print("error=\(error)")
return
}
print("response = \(response)")
}
task.resume()
Here are the Headers I need to add to this request:
X-ZUMO-APPLICATION: 45634243542434
ACCEPT: application/json
How do I attach these headers to my request?

Have a look at the reference docs:
https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSMutableURLRequest_Class/#//apple_ref/occ/instm/NSMutableURLRequest/setValue:forHTTPHeaderField:
so you would do: request.setValue("ACCEPT" forHTTPHeaderField: "application/json")

if you use alamofire, this should work, it also eases things for you so you choose get or post
var pars2 : Dictionary<String,String> = ["api_key":"value"]
Alamofire.request(.POST, "someURLString" ,parameters: pars2).responseJSON()
{
(request, response, data, error) in
if(data != nil)
{
self.countryIDArray.removeAll(keepCapacity: false)
var status = data!["status"]!! as! String
}
}

Related

Sending HTTP POST request with Swift to Discord Webhook results in Response Error

I'm trying to create a URL Request in Swift to send an HTTP POST request to a Discord Webhook, and the request completes in a failure defined as 'responseProblem'. I'm not sure where the response problem is originating from.
Discord should technically be able to accept HTTP requests, and a bunch of research into the issue has led me nowhere. I've worked with Discord webhooks before, but I've never played around with this kind of stuff before, so I'm kinda stuck for what I should do now.
import Foundation
enum APIError:Error {
case responseProblem
case decodingProblem
case encodingProblem
}
struct APIRequest {
let resourceURL: URL
init(endpoint: String) {
let resourceString = "<discord-webhook-url-removed-for-privacy>"
guard let resourceURL = URL(string: resourceString) else {fatalError()}
self.resourceURL = resourceURL
}
func save (_ messageToSave:Message, completion: #escaping(Result<Message, APIError>) -> Void ) {
do {
var urlRequest = URLRequest(url: resourceURL)
urlRequest.httpMethod = "POST"
urlRequest.addValue("application/json", forHTTPHeaderField: "Content-Type")
urlRequest.httpBody = try JSONEncoder().encode(messageToSave)
let dataTask = URLSession.shared.dataTask(with: urlRequest) { data, response, _ in
guard let httpResponse = response as? HTTPURLResponse, httpResponse.statusCode == 200, let
jsonData = data else {
completion(.failure(.responseProblem)) // <<< 'responseProblem' is outputted in console as the error
return
}
do {
let messageData = try JSONDecoder().decode(Message.self, from: jsonData)
completion(.success(messageData))
} catch {
completion(.failure(.decodingProblem))
}
}
dataTask.resume()
} catch {
completion(.failure(.encodingProblem))
}
}
}
When I run this Swift program, I expected it to send a request to the Discord Webhook to send a message into it's designated channel. However, the only error outputted into the console is responseProblem. Please let me know if there is anything further I need to add to get the root cause of the problem (I'm still semi-fresh to Swift, and I normally work with JavaScript, so I'm not sure how to properly debug in Swift and Xcode.)
Swift app is built in iOS 12.2 because Xcode doesn't like this stuff in iOS 13
This is a simplified version of how I post to a Discord webhook with Swift. From your post I can't see how you're converting your custom Message struct into a JSON dictionary, but you need at least the key/value pair for "content" to successfully post a Discord Webhook. And of course the posts can be customized in many other ways (with a custom "username" etc.).
var messageString: String = ""
guard let url = URL(string: "your-full-discord-webhook-url") else { return }
let messageJson: [String: Any] = ["content": messageString]
let jsonData = try? JSONSerialization.data(withJSONObject: messageJson)
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.addValue("application/json", forHTTPHeaderField: "content-type")
request.httpBody = jsonData
let task = URLSession.shared.dataTask(with: request)
task.resume()

Is there a different way how to send a HTTP "POST" request without using third party libraries using custom header and body?

I am trying to send a HTTP "POST" request for a web-service that should return a base64 encoded picture. This is an example HTTP request for the service:
I am trying the following:
func fetchPicture(username: String, password: String) {
let url = URL(string: "https://myurl.com/download/bootcamp/image.php")!
var request = URLRequest(url: url)
request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
request.httpMethod = "POST"
request.setValue(password.stringToSHA1Hash(), forHTTPHeaderField: "Authorization")
let postString = "username=\(username)"
request.httpBody = postString.data(using: .utf8)
let task = URLSession.shared.dataTask(with: request) { data, response, error in
guard let data = data, error == nil else { // check for fundamental networking error
print("error=\(error)")
return
}
if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode != 200 { // check for http errors
print("statusCode should be 200, but is \(httpStatus.statusCode)")
print("response = \(response)")
}
let responseString = String(data: data, encoding: .utf8)
print("responseString = \(responseString)")
}
task.resume()
}
I am getting an error 401 Unauthorized, I don't actually know whether it is because my request is bad all together or just the login initials. It would be grand if someone could go over the code and tell me if it actually corresponds to the request example shown above.
Thanks!
The first thing I notice is that you aren’t setting the request HTTP Method:
request.httpMethod = “POST”
As it turns out, I was using the CommonCrypto hashing function wrongly, I ended up using this instead:
https://github.com/apple/swift-package-manager/blob/master/Sources/Basic/SHA256.swift
And the SHA256 hash it returned was the correct one I needed, maybe this might help someone in the future.

How to get JSON data with HTTP Post method in swift 3?

I confuse by getting HTTP response in post method. when i check response on postman, then the required response is got and profile updated successfully. But in programmatically i got error notice. What was the problem? how to solve it? please, refer screenshots. Thanks in advance!
Code i have try
override func viewDidLoad() {
super.viewDidLoad()
self.updateDetails()
}
func updateDetails()
{
let postString = "api=update_people&user_id=18&email=rajesh#gmail.com&first_name=Raejsh&phone=456562&age=26&gender=male&blood_group=A"
print(postString)
// let alertMessage = alert()
let url = NSURL(string: "http://kuruthi.in/portal/api/register")
let request = NSMutableURLRequest(url: url as! URL)
request.httpBody = postString.data(using: String.Encoding.utf8)
request.httpMethod = "POST"
request.addValue("123456", forHTTPHeaderField: "X-API-KEY")
request.addValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
let task = URLSession.shared.dataTask(with: request as URLRequest) { data,response,error in
guard error == nil && data != nil else
{
print("Error:\(error)")
return
}
let httpStatus = response as? HTTPURLResponse
if httpStatus!.statusCode == 200
{
if data?.count != 0
{
let responseString = String(data: data!, encoding: .utf8)
print(responseString)
}
else
{
print("No data got from url!")
}
}
else
{
print("error httpstatus code")
}
}
task.resume()
}
Response in Program output :
Response in postman:
The reason you are getting html code is that you are receiving some error from your web services look at the message "A PHP Error was encountered" in your output log.
The success in postman is because your request is in form-data. Compare the request from postman from Raw with the request you are generating.
You can refer this link for creating a request. But the code is in Objective-Cand you can use this link to convert the code, or you can use Alamofire to create requests and refer this link for creating multipart form-data requests using Alamofire

How to use Alamofire to send bytes?

I'm creating an application to download my university timetable. I've done the REST calls in Java first to demonstrate a prototype which works nicely. And now I'd like to do it in Swift using Alamofire (or anything else which works).
Below is the REST call in Java that I'm trying to replicate.
Client client = Client.create();
String authString = "UID=XXXXX&PASS=XXXXX";
byte[] authBytes = authString.getBytes();
WebResource webResouce = client.resource("https://access.adelaide.edu.au/sa/login.asp");
ClientResponse response = webResource.post(ClientResponse.class, authBytes);
if (response.getStatus != 302) {
throw new RuntimeException("Failed: HTTP code: " + response.getStatus());
}
However I'm having trouble sending the bytes properly. The server will actually accept any byte data (so you can see if it works without a UID or PASS) and respond with 302, which indicates that it works. Otherwise it'll send a 200 which means it didn't.
I've had a few attempts of sending the UID and PASS in a parameter, getting their bytes and then putting them in a parameter etc etc. but nothing seems to work so far.
Any help would be great, thanks!
You should use Alamofire's custom encoding technique (something like this). This is my 3rd hour of Swift so bear with me.
struct ByteEncoding: ParameterEncoding {
private let data: Data
init(data: Data) {
self.data = data
}
func encode(_ urlRequest: URLRequestConvertible, with parameters: Parameters?) throws -> URLRequest {
var urlRequest = try urlRequest.asURLRequest()
urlRequest.httpBody = data
return urlRequest
}
}
Alamofire.request(url: "url", method: .post, parameters: nil, encoding: ByteEncoding(data: authBytesAsData)
Documentation
https://github.com/Alamofire/Alamofire#custom-encoding
If you use a regular NSURLRequest you can just set the request body:
let URL = NSURL(string: "https://access.adelaide.edu.au/sa/login.asp")!
let request = NSMutableURLRequest(URL: URL)
request.HTTPBody = // NSData you want as your body
Edit
As pointed out by #mattt himself, you can pass an NSURLRequest to Alamofire. No need for the hassle with custom parameter encoding as I answered first. (See below)
I don't exactly know how to do this using Alamofire, but it seems you can use a Custom parameter encoding with a closure. I didn't test this but took it from the Alamofire unit test source:
let encodingClosure: (URLRequestConvertible, [String: AnyObject]?) -> (NSURLRequest, NSError?) = { (URLRequest, parameters) in
let mutableURLRequest = URLRequest.URLRequest.mutableCopy() as NSMutableURLRequest
mutableURLRequest.HTTPBody = parameters["data"]
return (mutableURLRequest, nil)
}
let encoding: ParameterEncoding = .Custom(encodingClosure)
let URL = NSURL(string: "https://access.adelaide.edu.au/sa/login.asp")!
let URLRequest = NSURLRequest(URL: URL)
let data: NSData = // NSData you want as your body
let parameters: [String: AnyObject] = ["data": data]
let URLRequestWithBody = encoding.encode(URLRequest, parameters: parameters).0
Here's a quick example of how you could make this type of request.
import Alamofire
class BytesUploader {
func uploadBytes() {
let URLRequest: NSURLRequest = {
let URL = NSURL(string: "https://access.adelaide.edu.au/sa/login.asp")!
let mutableURLRequest = NSMutableURLRequest(URL: URL)
mutableURLRequest.HTTPMethod = "POST"
let authString = "UID=XXXXX&PASS=XXXXX"
let authData = authString.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!
mutableURLRequest.HTTPBody = authData
return mutableURLRequest.copy() as NSURLRequest
}()
let request = Alamofire.request(URLRequest)
request.response { request, response, data, error in
if let response = response {
println("Response status code: \(response.statusCode)")
if response.statusCode == 302 {
println("Request was successful")
} else {
println("Request was NOT successful")
}
} else {
println("Error: \(error)")
}
}
}
}
You need to encode your authorization string as an NSData object and then set that as the HTTPBody of the NSURLRequest. This should match your Java code that you posted.

How to send json object in swift without "key - value" pair format

Hey so I have to send a a token string to the Django-server and it only accepts one string. I am trying to use alamo fire to do this, however I cant send a key-value pair to resolve this problem. Is there an alternative solution for this. I am new IOS developer and this is my first project and I am new to the community. Thank you.
Convert your dictionary into a JSON string and ship it off that way:
func jsonStringify(data: AnyObject) -> NSData? {
var error: NSError?
if let json = NSJSONSerialization.dataWithJSONObject(
data,
options: NSJSONWritingOptions(0),
error: &error
) {
return json
} else {
return nil
}
}
Depending on how you need to send the token (POST vs GET vs HTTP Body vs Query String)... you might need to change the below. But it should get you started with NSURLSession. This will send the token 189E23FL2 to the server with POST as a HTTP Body parameter.
let url = NSURL(string: "http://some-server/endpoint")
var request = NSMutableURLRequest(URL: url!)
request.HTTPMethod = "POST"
request.HTTPBody = "189E23FL2".dataUsingEncoding(NSUTF8StringEncoding)
// if you need a csrf token, add something like this as well:
// request.addValue("the-csrf-token", forHTTPHeaderField: "X-CSRFToken")
var sessionConfiguration = NSURLSessionConfiguration.defaultSessionConfiguration()
var session = NSURLSession(configuration: sessionConfiguration)
var task = session.dataTaskWithRequest(request, completionHandler: { (data : NSData!, response : NSURLResponse!, error : NSError!) -> Void in
if (error == nil) {
println("Done!")
} else {
println("Errorororororor")
}
})
// start the task
task.resume()

Resources