Skip to content

Set custom HTTP request header for reporting #67

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

Merged
merged 1 commit into from
Jul 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import Foundation
extension NSMutableURLRequest {
static func makeJSONPost(
url: URL,
httpBody: Data
httpBody: Data,
additionalHTTPHeaders: [String: String]
) -> NSMutableURLRequest {
let request = NSMutableURLRequest(
url: url,
Expand All @@ -18,6 +19,10 @@ extension NSMutableURLRequest {
request.httpMethod = "POST"
request.setValue("application/json", forHTTPHeaderField: "Accept")
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
for keypair in additionalHTTPHeaders {
request.setValue(keypair.value, forHTTPHeaderField: keypair.key)
}

request.httpBody = httpBody

return request
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ class Reporter: NSObject {

var sessionID: String = UUID().uuidString
var url: URL
var additionalHTTPHeaders: [String: String] {
["x-litix-sdk": "swift-upload-sdk"]
}

// TODO: Set these using dependency Injection
var locale: Locale {
Expand Down Expand Up @@ -65,7 +68,8 @@ class Reporter: NSObject {

let request = NSMutableURLRequest.makeJSONPost(
url: url,
httpBody: httpBody
httpBody: httpBody,
additionalHTTPHeaders: additionalHTTPHeaders
)

guard let dataTask = session?.dataTask(
Expand Down Expand Up @@ -268,7 +272,8 @@ extension Reporter: URLSessionDelegate, URLSessionTaskDelegate {
// for any weirdness
let request = NSMutableURLRequest.makeJSONPost(
url: redirectURL,
httpBody: httpBody
httpBody: httpBody,
additionalHTTPHeaders: additionalHTTPHeaders
)

completionHandler(request as URLRequest)
Expand Down