我正在使用Xcode 8.3.3(8E3004b)
我正在使用TRON(包括Alamofire)向我的REST API发出HTTP请求。 我成功地获得了一个使用此设置的简单API。我正在尝试连接到另一个API,这需要我设置标头。正是这个API抛出了Status 415服务器错误。
我有以下代码通过TRON发出请求。根据{{3}},我应该像这样设置标题:
request.headers = ["Content-Type":"application/json"]
我也尝试过:
request.headerBuilder.headers(forAuthorizationRequirement: AuthorizationRequirement.allowed, including: ["Content-Type":"application/json"])
我尝试添加一些不同的写作方式,但似乎没有任何效果。
这是代码的更大部分,因此您可以看到上下文
let urlSubfix = "\(Constant.REST_MOBILE)\(Constant.REGISTER)"
let request: APIRequest<RegisterApiResult, JSONError> = tron.request(urlSubfix)
request.method = .put
// request.headers = ["Content-Type":"application/json"]
let header = request.headerBuilder.headers(forAuthorizationRequirement: AuthorizationRequirement.allowed, including: ["Content-Type":"application/json"])
request.headers = header
request.perform(withSuccess: { (registerApiResult) in
print("Successfully fetched our json object")
completion(registerApiResult)
}) { (err) in
print("Failed to fetch json...", err)
}
以下是我日志中的实际错误:
Failed to fetch json... APIError<JSONError>(request: Optional(http://www.slsdist.com/eslsd5/rest/mobileservice/register), response: Optional(<NSHTTPURLResponse: 0x618000028c20> { URL: http://www.slsdist.com/eslsd5/rest/mobileservice/register } { status code: 415, headers {
"Content-Length" = 0;
Date = "Sat, 22 Jul 2017 22:23:14 GMT";
Server = "Microsoft-IIS/7.5";
"X-Powered-By" = "Undertow/1, ASP.NET";
} }), data: Optional(0 bytes), error: Optional(Alamofire.AFError.responseValidationFailed(Alamofire.AFError.ResponseValidationFailureReason.unacceptableStatusCode(415))), errorModel: Optional(Go_Cart.Service.JSONError))
正如你所看到的,我试图以不同的方式设置标题,但它们似乎都没有起作用。任何人的帮助或建议都会有所帮助。
提前致谢。