Swift 2 How do you add authorization header to POST request

dbconfession

When making a particular POST request, Firefox (FF) dev tools shows a req. header named "Authorization" with a value of "Bearer X" where X is the access token received upon login. When I edit this request in FF and remove the "Authorization" line, I get a 400 error. When I put it back in, 200 and all is well. I haven't yet, however, figured out how to set this request header programmatically without getting 400.

Also, FF tools as a "Request Body" of {"source":"desktop - profile 2015"}. I'm assuming this is JSON. I've tried posting this in several ways (see code) but have had no success.

// the following fields are set in the object "Request"'s initialization
let accessToken = "1,2,3456789012,3x4f560fa7a89e01a2;33ab4b4e5e67e8e9b9f0e1a23db45678f9a9a0ff" // replaced some characters for this StackOF posting
let authorization = "Bearer \(accessToken)"
let method = "POST"
let userID = "1234567"
let URL = NSURL(string: "https://www.somesite.com/apitun/profile/\(userID)hide")

// tried setting params to all of the following 4:
let params = ""
let params = "&_json={}"
let params = "&_json={\"source\":\"desktop profile - 2015\"}
let params = "&_json=%7B%22source%22%3A%22desktop%2Dprofile%202015%22%7D"

func execute() {
    let session = NSURLSession.sharedSession()
    let request = NSMutableURLRequest(URL: URL)
    if authorization != "" {
        request.addValue(authorization, forHTTPHeaderField: "Authorization")
    }

    request.HTTPMethod = self.method
    request.HTTPBody = self.params.dataUsingEncoding(NSUTF8StringEncoding)
    self.task = session.dataTaskWithRequest(request) {
        (data, response, error) in
        NSHTTPCookieStorage.sharedHTTPCookieStorage().setCookies(self.cookies, forURL: self.URL, mainDocumentURL: nil)
        if error == nil {
            do {
                self.responseHeaders = response as! NSHTTPURLResponse
                self.cookies = NSHTTPCookieStorage.sharedHTTPCookieStorage().cookiesForURL(self.URL)!
                self.statusCode = self.responseHeaders.statusCode

                switch self.statusCode {

                case 200:
                    self.contentsOfURL = try NSString(contentsOfURL: self.URL, encoding: NSUTF8StringEncoding)
                case 400:
                    print("400: page not found")

                case 404:
                    print("404: page not found")

                case 407:
                    print("407: failed authenticate proxy credentials")

                default:
                    print("unable to get statusCode")

                }
            } catch {

            }
            self.isRequesting = false
        } else {
            print(error)
        }
    }
    self.task.resume()
}   
ThE uSeFuL
let request = NSMutableURLRequest(URL: NSURL(string: fullURL)!)

let accessToken = "your access token"
request.setValue("Bearer \(accessToken)", forHTTPHeaderField: "Authorization")

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

How do you set the Content-Type header for an HttpClient request?

From Java

How do you add query parameters to a Dart http request?

From Dev

Authorization header missing in PHP POST request

From Dev

How do I execute an ajax post with request header in Angular

From Dev

How do you send a POST request for logging in, with Angular's $resource?

From Dev

How to add a request header in Nancyfx?

From Dev

How do you add background music in Swift

From Dev

How to send Authorization header with a request in Swagger UI?

From Dev

How do you do println() in swift 2

From Dev

Add body in Post Request with Authentication Header android

From Dev

How do you add a trailing slash to a PUT request in Backbone?

From Dev

How do I edit the authorization header of an HTTP GET request originating from a hyperlink (<a href> tag)

From Dev

Guzzle HTTP - add Authorization header directly into request

From Dev

How to add a "Authorization=Bearer" header with Indy in Delphi?

From Dev

How to add Authorization header with Retrofit2 + RxJava

From Dev

Python POST request dropping Authorization header

From Dev

Python requests: POST request dropping Authorization header

From Dev

Python requests: POST request dropping Authorization header

From Dev

AFNetworking 2: Authorization header not included in request

From Dev

Swift 2 How do you add authorization header to POST request

From Dev

How to add a "Authorization=Bearer" header with Indy in Delphi?

From Dev

How to POST request in Swift

From Dev

How to add Authorization header in getTileUrl for ImageMapType Javascript

From Dev

How to remove authorization header from request

From Dev

How do I add an HTTP Authorization header to a REST call in PeopleSoft?

From Dev

How to add authorization on header using Swift 3.0 + Alamofire 4.0

From Dev

Xamarin Android: How do i open add header and send post request in webview

From Dev

Add header to post request in string

From Dev

How to pass params and add authorization header in GET Request in Volley

Related Related

  1. 1

    How do you set the Content-Type header for an HttpClient request?

  2. 2

    How do you add query parameters to a Dart http request?

  3. 3

    Authorization header missing in PHP POST request

  4. 4

    How do I execute an ajax post with request header in Angular

  5. 5

    How do you send a POST request for logging in, with Angular's $resource?

  6. 6

    How to add a request header in Nancyfx?

  7. 7

    How do you add background music in Swift

  8. 8

    How to send Authorization header with a request in Swagger UI?

  9. 9

    How do you do println() in swift 2

  10. 10

    Add body in Post Request with Authentication Header android

  11. 11

    How do you add a trailing slash to a PUT request in Backbone?

  12. 12

    How do I edit the authorization header of an HTTP GET request originating from a hyperlink (<a href> tag)

  13. 13

    Guzzle HTTP - add Authorization header directly into request

  14. 14

    How to add a "Authorization=Bearer" header with Indy in Delphi?

  15. 15

    How to add Authorization header with Retrofit2 + RxJava

  16. 16

    Python POST request dropping Authorization header

  17. 17

    Python requests: POST request dropping Authorization header

  18. 18

    Python requests: POST request dropping Authorization header

  19. 19

    AFNetworking 2: Authorization header not included in request

  20. 20

    Swift 2 How do you add authorization header to POST request

  21. 21

    How to add a "Authorization=Bearer" header with Indy in Delphi?

  22. 22

    How to POST request in Swift

  23. 23

    How to add Authorization header in getTileUrl for ImageMapType Javascript

  24. 24

    How to remove authorization header from request

  25. 25

    How do I add an HTTP Authorization header to a REST call in PeopleSoft?

  26. 26

    How to add authorization on header using Swift 3.0 + Alamofire 4.0

  27. 27

    Xamarin Android: How do i open add header and send post request in webview

  28. 28

    Add header to post request in string

  29. 29

    How to pass params and add authorization header in GET Request in Volley

HotTag

Archive