Apollo-ios: No ability to customize Content-Type header

Created on 31 Oct 2019  路  5Comments  路  Source: apollographql/apollo-ios

We need to be able to set the "Content-Type" header as "application/json; charset=utf-8", but the client is hardcoded to set "Content-Type" as "application/json".

This header is set here and overwritten for multi-part requests, but we need this charset added to the header for non-multi-part requests.

question

All 5 comments

There's the HTTPNetworkTransportPreflightDelegate which you can implement to modify or add additional headers;

class GraphQLClient {
    init() {
        let transport = HTTPNetworkTransport(url: "https://...", delegate: self)
        let client = ApolloClient(networkTransport: transport)
        // ....
    }
}

extension GraphQLClient: HTTPNetworkTransportPreflightDelegate {
    func networkTransport(_ networkTransport: HTTPNetworkTransport, shouldSend request: URLRequest) -> Bool {
        return true
    }

    func networkTransport(_ networkTransport: HTTPNetworkTransport, willSend request: inout URLRequest) {
        request.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")
    }
}

This delegate is called after all other modifications to the request have been done;

https://github.com/apollographql/apollo-ios/blob/3f3111d0ab1334fd344691ac95f163a2a78ab3c6/Sources/Apollo/HTTPNetworkTransport.swift#L419-L427

Yep @koenpunt is exactly correct, that's where you should make this change.

@spencer-microsoft Did this help you out?

Since I haven't heard back and there is a definite way to do this, I'm going to go ahead and close this issue out. Please reopen if you are still having problems after trying the above suggestions. Thank you!

Confirmed that works, thanks!

Was this page helpful?
0 / 5 - 0 ratings