Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How do I add a value to an http header? #27

Closed
ericjim opened this issue Aug 5, 2014 · 21 comments
Closed

How do I add a value to an http header? #27

ericjim opened this issue Aug 5, 2014 · 21 comments

Comments

@ericjim
Copy link

ericjim commented Aug 5, 2014

I am interested in adding an additional value to the following GET request's header:

let galleryRequest = Alamofire.request(.GET, "https://api.imgur.com/3/gallery.json")
    .responseString { (request, response, string, error) in
         // Do something...
    }

The header in question is: Authorization: Client-ID YOUR_CLIENT_ID where the client_id would be my imgur client_id.

Thanks for the awesome library!

@mattt
Copy link
Sponsor Contributor

mattt commented Aug 5, 2014

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
         // ...
}

@mattt mattt closed this as completed Aug 5, 2014
@mattt
Copy link
Sponsor Contributor

mattt commented Aug 5, 2014

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.

@Jeehut
Copy link

Jeehut commented Aug 13, 2014

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.

@Isuru-Nanayakkara
Copy link

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

@endel
Copy link

endel commented Jan 11, 2015

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

@seenickcode
Copy link

+1 @endel

3 similar comments
@longbowww
Copy link

+1 @endel

@slmcmahon
Copy link

+1 @endel

@nunogoncalves
Copy link

+1 @endel

@gctmadhumitha
Copy link

@mileswd
Copy link

mileswd commented Mar 12, 2015

+1 @endel

@punkeel
Copy link

punkeel commented Mar 16, 2015

👍 too

@clarisli
Copy link

clarisli commented Apr 2, 2015

+1 @endel

1 similar comment
@whusted
Copy link

whusted commented Apr 3, 2015

+1 @endel

@blakeperdue
Copy link

+1

@crossle
Copy link

crossle commented Apr 15, 2015

+1, so important feature, but...

@tobinharris
Copy link

+1 @endel

@Pavan-Concur
Copy link

+1

2 similar comments
@embassem
Copy link

+1

@ghost
Copy link

ghost commented Apr 26, 2015

+1

@tobinharris
Copy link

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)
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests