diff --git a/AmplifyPlugins/API/Sources/AWSAPIPlugin/Interceptor/RequestInterceptor/AuthTokenURLRequestInterceptor.swift b/AmplifyPlugins/API/Sources/AWSAPIPlugin/Interceptor/RequestInterceptor/AuthTokenURLRequestInterceptor.swift index 0175364b7a..9d2589d3bc 100644 --- a/AmplifyPlugins/API/Sources/AWSAPIPlugin/Interceptor/RequestInterceptor/AuthTokenURLRequestInterceptor.swift +++ b/AmplifyPlugins/API/Sources/AWSAPIPlugin/Interceptor/RequestInterceptor/AuthTokenURLRequestInterceptor.swift @@ -32,8 +32,6 @@ struct AuthTokenURLRequestInterceptor: URLRequestInterceptor { mutableRequest.setValue(amzDate, forHTTPHeaderField: URLRequestConstants.Header.xAmzDate) - mutableRequest.setValue(URLRequestConstants.ContentType.applicationJson, - forHTTPHeaderField: URLRequestConstants.Header.contentType) mutableRequest.setValue(userAgent, forHTTPHeaderField: URLRequestConstants.Header.userAgent) diff --git a/AmplifyPlugins/API/Sources/AWSAPIPlugin/Interceptor/RequestInterceptor/IAMURLRequestInterceptor.swift b/AmplifyPlugins/API/Sources/AWSAPIPlugin/Interceptor/RequestInterceptor/IAMURLRequestInterceptor.swift index 013cac0459..0a9b53fe93 100644 --- a/AmplifyPlugins/API/Sources/AWSAPIPlugin/Interceptor/RequestInterceptor/IAMURLRequestInterceptor.swift +++ b/AmplifyPlugins/API/Sources/AWSAPIPlugin/Interceptor/RequestInterceptor/IAMURLRequestInterceptor.swift @@ -36,7 +36,6 @@ struct IAMURLRequestInterceptor: URLRequestInterceptor { throw APIError.unknown("Could not get host from mutable request", "") } - request.setValue(URLRequestConstants.ContentType.applicationJson, forHTTPHeaderField: URLRequestConstants.Header.contentType) request.setValue(host, forHTTPHeaderField: "host") request.setValue(userAgent, forHTTPHeaderField: URLRequestConstants.Header.userAgent) diff --git a/AmplifyPlugins/API/Sources/AWSAPIPlugin/Operation/AWSRESTOperation.swift b/AmplifyPlugins/API/Sources/AWSAPIPlugin/Operation/AWSRESTOperation.swift index ca2324f4a5..1c8efc986d 100644 --- a/AmplifyPlugins/API/Sources/AWSAPIPlugin/Operation/AWSRESTOperation.swift +++ b/AmplifyPlugins/API/Sources/AWSAPIPlugin/Operation/AWSRESTOperation.swift @@ -116,9 +116,6 @@ final public class AWSRESTOperation: AmplifyOperation< } } - // The headers from the request object should override any of the same header modifications done by the intercepters above - finalRequest = RESTOperationRequestUtils.applyCustomizeRequestHeaders(request.headers, on: finalRequest) - if isCancelled { finish() return diff --git a/AmplifyPlugins/API/Sources/AWSAPIPlugin/Support/Utils/RESTOperationRequestUtils.swift b/AmplifyPlugins/API/Sources/AWSAPIPlugin/Support/Utils/RESTOperationRequestUtils.swift index a553dcef71..8a9716def3 100644 --- a/AmplifyPlugins/API/Sources/AWSAPIPlugin/Support/Utils/RESTOperationRequestUtils.swift +++ b/AmplifyPlugins/API/Sources/AWSAPIPlugin/Support/Utils/RESTOperationRequestUtils.swift @@ -57,31 +57,19 @@ final class RESTOperationRequestUtils { headers: [String: String]?, requestPayload: Data?) -> URLRequest { - let baseHeaders = ["content-type": "application/json"] var baseRequest = URLRequest(url: url) - baseRequest = applyCustomizeRequestHeaders( - baseHeaders.merging(headers ?? [:], uniquingKeysWith: { _, new in new }), - on: baseRequest - ) + var requestHeaders = ["content-type": "application/json"] + if let headers = headers { + for (key, value) in headers { + requestHeaders[key.lowercased()] = value + } + } + baseRequest.allHTTPHeaderFields = requestHeaders baseRequest.httpMethod = operationType.rawValue baseRequest.httpBody = requestPayload return baseRequest } - static func applyCustomizeRequestHeaders(_ headers: [String: String]?, on request: URLRequest) -> URLRequest { - guard let headers = headers, - let mutableRequest = (request as NSURLRequest).mutableCopy() as? NSMutableURLRequest - else { - return request - } - - for (key, value) in headers { - mutableRequest.setValue(value, forHTTPHeaderField: key) - } - - return mutableRequest as URLRequest - } - private static let permittedQueryParamCharacters = CharacterSet.alphanumerics .union(.init(charactersIn: "/_-.~")) diff --git a/AmplifyPlugins/API/Tests/AWSAPIPluginTests/Interceptor/AuthTokenURLRequestInterceptorTests.swift b/AmplifyPlugins/API/Tests/AWSAPIPluginTests/Interceptor/AuthTokenURLRequestInterceptorTests.swift index ed1df2905b..a0ef11879a 100644 --- a/AmplifyPlugins/API/Tests/AWSAPIPluginTests/Interceptor/AuthTokenURLRequestInterceptorTests.swift +++ b/AmplifyPlugins/API/Tests/AWSAPIPluginTests/Interceptor/AuthTokenURLRequestInterceptorTests.swift @@ -15,7 +15,12 @@ class AuthTokenURLRequestInterceptorTests: XCTestCase { func testAuthTokenInterceptor() async throws { let mockTokenProvider = MockTokenProvider() let interceptor = AuthTokenURLRequestInterceptor(authTokenProvider: mockTokenProvider) - let request = URLRequest(url: URL(string: "http://anapiendpoint.ca")!) + let request = RESTOperationRequestUtils.constructURLRequest( + with: URL(string: "http://anapiendpoint.ca")!, + operationType: .get, + headers: nil, + requestPayload: nil + ) guard let headers = try await interceptor.intercept(request).allHTTPHeaderFields else { XCTFail("Failed retrieving headers") diff --git a/AmplifyPlugins/API/Tests/AWSAPIPluginTests/Support/Utils/RESTRequestUtilsTests.swift b/AmplifyPlugins/API/Tests/AWSAPIPluginTests/Support/Utils/RESTRequestUtilsTests.swift index 4de8148fcc..8e1e5ec1ef 100644 --- a/AmplifyPlugins/API/Tests/AWSAPIPluginTests/Support/Utils/RESTRequestUtilsTests.swift +++ b/AmplifyPlugins/API/Tests/AWSAPIPluginTests/Support/Utils/RESTRequestUtilsTests.swift @@ -95,18 +95,6 @@ class RESTRequestUtilsTests: XCTestCase { ) ) } - - func testApplyCustomizeRequestHeaders_withCutomeHeaders_successfullyOverride() { - var request = URLRequest(url: URL(string: "https://aws.amazon.com")!) - request.allHTTPHeaderFields = ["Content-Type": "application/json"] - let headers = ["Content-Type": "text/plain"] - let requestWithHeaders = RESTOperationRequestUtils.applyCustomizeRequestHeaders(headers, on: request) - XCTAssertNotNil(requestWithHeaders.allHTTPHeaderFields) - for (key, value) in headers { - XCTAssertEqual(requestWithHeaders.allHTTPHeaderFields![key], value) - } - } - } extension RESTRequestUtilsTests {