Comments (21)
For anything beyond what the shorthand provides, you'll have to drop down a level of abstraction to create and manage NSURLRequest
objects yourself. This may change in a future version, but for now, here's the process:
let URL: NSURL(string: "http://api.imgur.com/3/gallery.json")
var mutableURLRequest = NSMutableURLRequest(URL: URL)
mutableURLRequest.setValue("...", forHTTPHeaderField: "Authorization")
let manager = Alamofire.Manager.sharedInstance // or create a new one
let request = manager.request(mutableURLRequest)
request.responseString { (request, response, string, error) in
// ...
}
from alamofire.
Alternatively, you could also set the Authorization
on the defaultHeaders
of a manager. This would automatically apply the header to all requests created through method shorthand.
Real documentation for this usage is forthcoming.
from alamofire.
Same comment as in #7 applies here as well. Would be great if new features weren't just closed but marked as new features. Thank you.
from alamofire.
@mattt Hi! Is your example valid for POST requests as well? I tried it but it returns 405 - method not allowed error.
I tried explicitly specifying the http method in the url request like this,
mutableRequest.HTTPMethod = "POST"
Then I got a 500 - internal server error.
EDIT: Yes, I'm sending it as a GET request. That's the cause of the 405 error. I tested it using hurl.it. Please show how to add a http header of a POST request as well? Also how do I add normal parameters to this POST request?
Attempt: Okay, I got it working. The reason I got the 500 error was because I had forgotten to append the parameter. I accomplished it by adding these few extra lines.
mutableRequest.HTTPMethod = "POST"
let postString = "UniqueId=\(uniqueID)"
mutableRequest.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding)
Now it works fine. I hope you're working on something to simplify this process. :)
from alamofire.
I would like to re-open this issue. Sending additional request headers is mandatory on any REST API.
from alamofire.
+1 @endel
from alamofire.
+1 @endel
from alamofire.
+1 @endel
from alamofire.
+1 @endel
from alamofire.
- 1 @endel
from alamofire.
+1 @endel
from alamofire.
👍 too
from alamofire.
+1 @endel
from alamofire.
+1 @endel
from alamofire.
+1
from alamofire.
+1, so important feature, but...
from alamofire.
+1 @endel
from alamofire.
+1
from alamofire.
+1
from alamofire.
+1
from alamofire.
For custom headers and POST
parameters I used something like this (Swift). Hope it helps.
/// Creates a POST to the API, returning an Alamofire request that
/// you can work with. Does NOT MAKE THE CALL
/// :param: params a dictionary of parameters to add to the body
func post(url: String, params: Dictionary<String, AnyObject>) -> Request {
let URL = NSURL(string: url)!
let req = NSMutableURLRequest(URL: URL)
req.HTTPMethod = "POST"
req.setValue("some-value", forHTTPHeaderField: "MyCustomHeader")
req.HTTPBody = params.toURLString().dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: true)
return Alamofire.request(req).validate(statusCode: 200 ..< 300)
}
from alamofire.
Related Issues (20)
- Facing issues on iPhone, the Alamofire response is not activated and Alamofire task session internal createTask event does not execute. HOT 2
- override AuthenticationInterceptor HOT 1
- Add SPM product as a dynamic framework HOT 3
- Provide Privacy Manifest support for AFNetworking HOT 22
- Multiple AF.upload cannot be concurrent HOT 1
- App crash on archived version HOT 1
- Different iOS minimal OS support in Package.swift and Alamofire.podspec HOT 1
- Upload Progress called only once with fraction value 1.0 before the upload request complete HOT 2
- Dr
- Add Conformance To ClientTransport From swift-openapi-generator HOT 1
- Timeout responseCode is nil HOT 2
- Add support for visionOS HOT 1
- New version with privacy manifest is not available HOT 3
- Crash - CompositeEventMonitor.request<A>(_:didParseResponse:) HOT 1
- The macOS deployment target 'MACOSX_DEPLOYMENT_TARGET' is set to 10.10, but the range of supported deployment target versions is 10.13 to 14.2.99. (in target 'Alamofire macOS' from project 'Alamofire') HOT 1
- In Xcode's InspectorView, Signature's Kind is Unsigned HOT 1
- Crash on startup with Alamofire 4.9.1 HOT 1
- Cocoapods support for visionOS HOT 4
- Authenticator with Swift Concurrency HOT 3
- Almofire SDK privacy details not reflecting in my project HOT 20
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from alamofire.