Code Monkey home page Code Monkey logo

Comments (21)

mattt avatar mattt commented on May 22, 2024 2

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.

mattt avatar mattt commented on May 22, 2024

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.

Jeehut avatar Jeehut commented on May 22, 2024

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.

Isuru-Nanayakkara avatar Isuru-Nanayakkara commented on May 22, 2024

@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.

endel avatar endel commented on May 22, 2024

I would like to re-open this issue. Sending additional request headers is mandatory on any REST API.

from alamofire.

seenickcode avatar seenickcode commented on May 22, 2024

+1 @endel

from alamofire.

longbowww avatar longbowww commented on May 22, 2024

+1 @endel

from alamofire.

slmcmahon avatar slmcmahon commented on May 22, 2024

+1 @endel

from alamofire.

nunogoncalves avatar nunogoncalves commented on May 22, 2024

+1 @endel

from alamofire.

gctmadhumitha avatar gctmadhumitha commented on May 22, 2024

from alamofire.

mileswd avatar mileswd commented on May 22, 2024

+1 @endel

from alamofire.

punkeel avatar punkeel commented on May 22, 2024

👍 too

from alamofire.

clarisli avatar clarisli commented on May 22, 2024

+1 @endel

from alamofire.

whusted avatar whusted commented on May 22, 2024

+1 @endel

from alamofire.

blakeperdue avatar blakeperdue commented on May 22, 2024

+1

from alamofire.

crossle avatar crossle commented on May 22, 2024

+1, so important feature, but...

from alamofire.

tobinharris avatar tobinharris commented on May 22, 2024

+1 @endel

from alamofire.

Pavan-Concur avatar Pavan-Concur commented on May 22, 2024

+1

from alamofire.

embassem avatar embassem commented on May 22, 2024

+1

from alamofire.

 avatar commented on May 22, 2024

+1

from alamofire.

tobinharris avatar tobinharris commented on May 22, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.