Can anyone suggest some pointers for how to extend alamofire with headers, like Content-MD5 which need to be set right before it gets sent?
This is a bit old question, but I've had the same problem and I solved it using the following code:
Alamofire.request(.POST, "your-url", parameters: params, encoding: .Custom({(convertible, paramsOptinal) in
guard let params = paramsOptinal else {return (convertible.URLRequest, NSError(domain: "", code: -1, userInfo: nil))}
let mutableRequest = convertible.URLRequest.copy() as! NSMutableURLRequest
let json = try! NSJSONSerialization.dataWithJSONObject(params, options: NSJSONWritingOptions.PrettyPrinted)
let jsonMd5 = self.MD5(json).base64EncodedStringWithOptions(NSDataBase64EncodingOptions(rawValue: 0))
mutableRequest.setValue(jsonMd5, forHTTPHeaderField: "Content-MD5")
let contentType = "application/json; charset=utf-8"
mutableRequest.setValue(contentType, forHTTPHeaderField: "Content-Type")
mutableRequest.HTTPBody = json
return (mutableRequest, nil)}
)).responseString {response in
print(response)
}
Hope you find this helpful.
Omer
Related
Do you know what is the equivalent for Swift 3.1 with the following Objective-C 2.0 code:
theErr = AudioFileOpenURL((__bridge CFURLRef)audioURL, kAudioFileReadPermission, 0, &audioFile);
Any clues?
If you use autocompletion, it will hold your hand through this process.
And when you get to that field, hit enter, and then go to the end, enter a . (as it's clearly an enumeration), and you can see the options:
But it is:
var audioURL: URL = ...
var audioFileID: AudioFileID?
let status = AudioFileOpenURL(audioURL as CFURL, .readPermission, .allZeros, &audioFileID)
CFURLRef has been renamed to CFURL. URL is toll-free bridged to CFURL, all you need to do is cast an url to CFURL using as.
Here's the updated code for Swift 4:
let audioURL: URL = ...
var audioFile: AudioFileID?
let status = AudioFileOpenURL(audioURL as CFURL,
.readPermission,
0,
&audioFile)
Getting error while calling Alamofire request method in the latest version(4.0.0).
The syntax is:
Alamofire.request(urlString,method: .post, parameters: requestParams, encoding: .JSON, headers: [:])
the type of requestParam is [String:Any]
I got the issue, I have to use JSONEncoding.default instead of .JSON, so the new syntax is
Alamofire.request(urlString,method: .post, parameters: requestParams, encoding: JSONEncoding.default, headers: [:])
I can only refer you to: https://github.com/Alamofire/Alamofire/issues/1508#issuecomment-246207682
Basically, if one of your parameters is of the wrong type, the swift compiler will assume you're using request(urlRequest:URLRequestConvertible) and then, the method is an extra argument
Go over that parameters again and make sure all is of correct type (Parameters?, ParameterEncoding, and HTTPHeaders)
I was having the same issue, the problem is at parameters type, it should be of type [String: Any]. After I made this change, it worked for me.
Alamofire.request(youUrl, method: .post, parameters: param as? [String: Any], encoding: JSONEncoding.default, headers: [:])
.responseJSON { response in
It means that some of the parameters type are wrong, check that you are sending these values:
url: String
method: HTTPMethod (E.g: .post)
parameters: [String:Any]
encoding: ParameterEncoding (E.g: JSONEncoding.default)
headers: [String: String]
Updated for Swift 3:
let requestString = "https://thawing-inlet-46474.herokuapp.com/charge.php"
let params = ["stripeToken": token.tokenId, "amount": "200", "currency": "usd", "description": "testRun"]
Alamofire.request(requestString,method: .post, parameters: params, encoding: JSONEncoding.default, headers: [:]).responseJSON { (response:DataResponse<Any>) in
switch(response.result) {
case .success(_):
if response.result.value != nil{
print("response : \(response.result.value)")
}
break
case .failure(_):
print("Failure : \(response.result.error)")
break
}
}
Make sure your parameters is [String: Any]
i.e
let parameters = ["foo": "bar"]
Not:
let parameters : Parameter = ["foo": "bar"]
You are getting that error because of the wrong data types.
Parameters Type should be [String : Any] and parameters type shouldn't be an optional.
Header Type should be [String : String] or nil and header type shouldn't be an optional as well.
Hope it helps. :-)
I fixed this by ensuring my requestUrls are strings, and not URL types. Also I removed parameter arguments for when parameter was nil.
E.g.
Alamofire.request(requestUrl, method: .get, encoding: URLEncoding.default)
Almofire methods changed in Swift 3 as the following:
Reorder parameters (url then method type).
Change Encoding Enum to be "JSONEncoding.default" for example.
This is a family of functions that are very similar, which makes the compiler think you're trying to use a different function. Double check that ALL of the arguments you're supplying are the CORRECT TYPE that is expected. For example I was passing [String:Any] for the variant with headers, which expected [String:String] and got the same error.
It is always because im using optional variables in any of the parameters
I was facing same problem And try with all answer as previously post here, And then I got the solution and reason of this problem .
This is happened due to pass the wrong object parse in the request, and finally the solution --
theJSONText -- JSON string
urlStr -- URL string
let urlEncoadedJson = theJSONText.addingPercentEncoding(withAllowedCharacters:.urlHostAllowed)
let urls = NSURL(string:"urlStr\(urlEncoadedJson ?? "")")
let method: HTTPMethod = .get
Alamofire.request(urls! as URL, method: method, parameters: nil, encoding: JSONEncoding.default, headers: nil).responseJSON { (response:DataResponse<Any>) in
switch(response.result) {
case .success(_):
if response.result.value != nil
{
// write your code here
}
break
case .failure(_):
if response.result.error != nil
{
print(response.result.error!)
}
break
}
}
Note - There is no param in my URL .
My problem was in optional parameters in the headers that were not unwrapped.
Alamofire.request(url, method: .post, parameters: parameters, encoding:
JSONEncoding.default, headers: [:]).responseJSON
{ response in
switch (response.result) {
case .success:
print(response)
break
case .failure:
print(Error.self)
}
}
just make sure all the parameters are in the correct format , most importantly the parameters must be in [String:Any] type array.
I am facing an issue while uploading image in Amazon AWS. Here is my code:
import UIKit
protocol ContentUploaderDelegate {
func onContentLoadComplete(status:Bool,serverResponse:String)
}
class ContentUploader
{
let contentURL = "https:<MY URL>amazonaws.com/api/v1/contents"
var delegate:ContentUploaderDelegate?
func uploadImage(image:UIImage,xAuth:String,mimeType:String,imageName:String)
{
let url = NSURL(string: contentURL)
let request = NSMutableURLRequest(URL: url!)
request.HTTPMethod = "POST"
let boundary = generateBoundaryString()
//define the multipart request type
request.setValue("multipart/form-data; boundary=\(boundary)", forHTTPHeaderField: "Content-Type")
request.setValue(xAuth, forHTTPHeaderField: "x-auth-token")
request.setValue("application/json", forHTTPHeaderField: "accept")
let image_data = UIImageJPEGRepresentation(image, 1.0)
if(image_data == nil)
{
return
}
let body = NSMutableData()
//name to save in server
let fname = imageName
let mimetype = mimeType
//define the data post parameter
body.appendData("--\(boundary)\r\n".dataUsingEncoding(NSUTF8StringEncoding)!)
body.appendData("Content-Disposition:form-data; name=\"test\"\r\n\r\n".dataUsingEncoding(NSUTF8StringEncoding)!)
body.appendData("hi\r\n".dataUsingEncoding(NSUTF8StringEncoding)!)
body.appendData("--\(boundary)\r\n".dataUsingEncoding(NSUTF8StringEncoding)!)
body.appendData("Content-Disposition:form-data; name=\"files\"; filename=\"\(fname)\"\r\n".dataUsingEncoding(NSUTF8StringEncoding)!)
body.appendData("Content-Type: \(mimetype)\r\n\r\n".dataUsingEncoding(NSUTF8StringEncoding)!)
body.appendData(image_data!)
body.appendData("\r\n".dataUsingEncoding(NSUTF8StringEncoding)!)
body.appendData("--\(boundary)--\r\n".dataUsingEncoding(NSUTF8StringEncoding)!)
//set the HTTPBody
request.HTTPBody = body
let session = NSURLSession.sharedSession()
let task = session.dataTaskWithRequest(request) {
(
let data, let response, let error) in
guard let _:NSData = data, let _:NSURLResponse = response where error == nil else {
print("error")
self.delegate?.onContentLoadComplete(false, serverResponse: (error?.description)!)
return
}
let dataString = NSString(data: data!, encoding: NSUTF8StringEncoding)
print("success \(dataString)")
self.delegate?.onContentLoadComplete(true, serverResponse:dataString! as String)
}
task.resume()
}
private func generateBoundaryString() -> String
{
return "Boundary-\(NSUUID().UUIDString)"
}
The following delegate method never gets called. What could be the reason?
func URLSession(session: NSURLSession,
task: NSURLSessionTask,
didReceiveChallenge challenge: NSURLAuthenticationChallenge,
completionHandler: (NSURLSessionAuthChallengeDisposition, NSURLCredential?)
-> Void) {
let protectionSpace = challenge.protectionSpace
let theSender = challenge.sender
if protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust {
if (challenge.protectionSpace.host == "ec2-52-36-216-81.us-west-2.compute.amazonaws.com") {
if let theTrust = protectionSpace.serverTrust{
let theCredential = NSURLCredential(trust: theTrust)
theSender!.useCredential(theCredential, forAuthenticationChallenge: challenge)
return
}
}
}
theSender!.performDefaultHandlingForAuthenticationChallenge!(challenge)
return
}
}
And I am getting the following error. Any idea why getting this error?
Error Domain=NSURLErrorDomain Code=-1202 "The certificate for this
server is invalid. You might be connecting to a server that is
pretending to be “.amazonaws.com” which could put your
confidential information at risk."
UserInfo={NSURLErrorFailingURLPeerTrustErrorKey=, NSLocalizedRecoverySuggestion=Would you like to
connect to the server anyway?, _kCFStreamErrorDomainKey=3,
_kCFStreamErrorCodeKey=-9813, NSErrorPeerCertificateChainKey={type = immutable, count = 1, values = (
0 : .com i: www..com> )}, NSUnderlyingError=0x7f9d42aedc10 {Error
Domain=kCFErrorDomainCFNetwork Code=-1202 "(null)"
UserInfo={_kCFStreamPropertySSLClientCertificateState=0,
kCFStreamPropertySSLPeerTrust=,
_kCFNetworkCFStreamSSLErrorOriginalValue=-9813, _kCFStreamErrorDomainKey=3, _kCFStreamErrorCodeKey=-9813, kCFStreamPropertySSLPeerCertificates={type = immutable, count = 1, values = (
0 : .com i: www..com> )}}}, NSLocalizedDescription=The certificate for this server is
invalid. You might be connecting to a server that is pretending to be
“.amazonaws.com” which could put your confidential information
at risk., NSErrorFailingURLKey=https://amazonaws.com/api/v1/contents,
NSErrorFailingURLStringKey=https://.amazonaws.com/api/v1/contents,
NSErrorClientCertificateStateKey=0}
Error Domain=NSURLErrorDomain Code=-1202 "The certificate for this server is invalid. You might be connecting to a server that is pretending to be “.amazonaws.com”
I believe something is wrong with the common name “.amazonaws.com”
NSErrorFailingURLKey=https://amazonaws.com/api/v1/contents,
NSErrorFailingURLStringKey=https://.amazonaws.com/api/v1/contents
The URLs shown in the error message do not appear to be a well know endpoint. I would expect to see something like https://ec2-2-2-2-2.compute-1.amazonaws.com or another Fully Qualified Domain name there.
The error message also confirms this. You are connecting a host, but the name on the certificate does not match. This is the reason for the pretending to be “.amazonaws.com” error.
Confirm the correct endpoint, and how your code is forming the full URL.
The following delegate method never gets called. What could be the
reason?
The error occurs before the function is called. The session is never established because of the certificate error.
Hi I am new to swift please spare me.
I need to post to particular API but the api is not a fan of key value pair the api expect raw json as post data
I use this library here to make post request.
this is my code
func postItem(itemname: String, itemnumber: Int, itemcode:String, url:String, baseURL:String, completion: (result: Dictionary<String, AnyObject>) -> ()){
var dict: Dictionary<String, AnyObject>!
var params: Dictionary<String,AnyObject> = ["parentItem": ["itemname":itemname,"itemnumber":itemnumber,"itemcode":code]]
let data = NSJSONSerialization.dataWithJSONObject(params, options: NSJSONWritingOptions.PrettyPrinted, error: nil)
let string = NSString(data: data!, encoding: NSUTF8StringEncoding)
var request = HTTPTask()
request.requestSerializer = JSONRequestSerializer()
request.requestSerializer.headers[headerKey] = getToken() //example of adding a header value
request.POST(url, parameters: params, success: {(response: HTTPResponse) in
if response.responseObject != nil {
let data = response.responseObject as NSData
var error: NSError?
dict = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: &error) as Dictionary<String, AnyObject>;
completion(result: dict)
}
},failure: {(error: NSError, response: HTTPResponse?) in
dict = ["error" : "error" ]
completion(result: dict)
})
}
i need to pass this kind of raw json in api
eg. {"parentItem": {"itemname":"Cocoa","itemnumber":123,"itemcode":"cocoa-12-A"}}
but when I println my params because it is dictionary it generate something like
["parentItem": ["itemname"="Cocoa"; "itemnumber"=123; "itemcode"="cocoa-12-A"]]
I just couldn't convert the params to JSON because the library I'm using is expecting dictionary and I'm having a hard time creating my own class.
could anyone help me? any comments and suggestion would do. Thanks in advance.
Why don't use Alamofire framework ? It's pretty good and sends standard json
I hope to find some help to diving deeper into Podiokit, the ObjC-API to Podio. I try to set a link-field's value to a URL. My first simple try looked like this:
NSDictionary *embedAttributes = [NSDictionary dictionaryWithObject: #"http://www.google.com" forKey: #"url"];
PKTEmbed *embed = [[PKTEmbed alloc] initWithDictionary: embedAttributes];
item[#"linkfield"] = embed;
I found an example using PHP but had no luck to transform it into Objective-C:
$attributes = array( 'url' => 'http://www.infranet.com' );
$embed = PodioEmbed::create( $attributes );
$attribute['embed']['embed\_id'] = $embed->embed\_id;
$attribute['file']['file\_id'] = $embed->files[0]->file\_id;
$this->orgItem->field('organizationlink')->set\_value($attribute);
Maybe someone knows how to get it right, would be fine :-)
[Edit] The PodioKit-Manual just says:
PKTEmbed *link = ...;
item[#"link"] = link;
[Edit 2] The error occurs when I try to save the item. The log says:
Error: Saving file Error Domain=com.alamofire.error.serialization.response Code=-1011 "Request failed: Ungültige Anforderung (400)" UserInfo=0x600000c7ee80 {com.alamofire.serialization.response.error.response=<NSHTTPURLResponse: 0x6000008358e0> { URL: https://api.podio.com/item/app/_xxxx_/ } { status code: 400, headers {
"Content-Length" = 263;
"Content-Type" = "application/json; charset=utf-8";
Date = "Sat, 27 Sep 2014 19:16:22 GMT";
Server = nginx;
"X-Podio-Request-Id" = yqyl6yku;
"X-Rate-Limit-Limit" = 250;
"X-Rate-Limit-Remaining" = 248;
} }, NSLocalizedDescription=Request failed: Ungültige Anforderung (400), NSErrorFailingURLKey=https://api.podio.com/item/app/_xxxx_/}
Thanks,
Michael / Hamburg
Sebastian at Podio here. You need to first create the PKTEmbed object server side, then use it as the value of the item field. So you would use:
PKTItem *item = ...;
[[PKTEmbed createEmbedForURLString:#"https://www.google.com"] onSuccess:^(PKTEmbed *embed) {
item[#"link-field"] = embed;
} onError:^(NSError *error) {
// Handle error
}];
The server will assign you an embedID and generate a thumbnail for you etc. I will look into adding the ability to simply provide a URL string directly, as I agree that makes a lot of sense.
Hope that helps!