Search code examples
swiftparsingafnetworkingalamofiregraphql

How to send content type in a request while doing parsing using Alamofire networking in Swift?


I am new to Swift. I want to parse a graphql webservice where i need to send the content type to header as "application/graphql". In general in Objective-C if we are using AFNetworking we can set up the content type as below.

[requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];

In Swift i am using Alamofire, i don't have any idea regarding this, that how to set the content type. Being a new comer for Swift, please advised me without hesitation.

One more thing is graphql service don't accept a proper JSON, Please refer here

Please suggest me how to post the values to server? by preparing a string is only an answer? (Sorry if i misunderstood graphql). But please don't hesitate. Give your valuable suggestions.Thanks.


Solution

  • To pass content type in header with the Alamofire you can try the following code.

    // Step : 1  
        var manager = Manager.sharedInstance
    
        // Specifying the Headers we need
        manager.session.configuration.HTTPAdditionalHeaders = [
            "Content-Type": "application/graphql",
            "Accept": "application/json" //Optional
            ]
    
       // Step : 3 then call the Alamofire request method.
    
     Alamofire.request(.GET, url2).responseJSON { request, response,  result in
         print(result.value)
      }