如何在 Swift 中使用 Alamofire 网络进行解析时在请求中发送内容类型?
How to send content type in a request while doing parsing using Alamofire networking in Swift?
我是 Swift 的新手。我想解析一个 graphql 网络服务,我需要将内容类型发送到 header 作为“application/graphql”。一般来说 Objective-C 如果我们使用 AFNetworking 我们可以设置内容类型如下。
[requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
在 Swift 中,我正在使用 Alamofire,对此我不知道如何设置内容类型。作为Swift的新人,请多多指教
还有一点是 graphql 服务不接受正确的 JSON,请参考 here
请建议我如何将值 post 发送到服务器?通过准备一个字符串只是一个答案? (抱歉,如果我误解了 graphql)。但请不要犹豫。献出你宝贵的suggestions.Thanks.
要使用 Alamofire 在 header 中传递内容类型,您可以尝试以下代码。
// 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)
}
我是 Swift 的新手。我想解析一个 graphql 网络服务,我需要将内容类型发送到 header 作为“application/graphql”。一般来说 Objective-C 如果我们使用 AFNetworking 我们可以设置内容类型如下。
[requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
在 Swift 中,我正在使用 Alamofire,对此我不知道如何设置内容类型。作为Swift的新人,请多多指教
还有一点是 graphql 服务不接受正确的 JSON,请参考 here
请建议我如何将值 post 发送到服务器?通过准备一个字符串只是一个答案? (抱歉,如果我误解了 graphql)。但请不要犹豫。献出你宝贵的suggestions.Thanks.
要使用 Alamofire 在 header 中传递内容类型,您可以尝试以下代码。
// 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)
}