Swift 这里通过邮递员和URLSession获取的数据不同http://173.249.20.137:9000/apiapp/coupon

Swift 这里通过邮递员和URLSession获取的数据不同http://173.249.20.137:9000/apiapp/coupon,swift,postman,urlsession,Swift,Postman,Urlsession,获取方法 当我通过URLSession和Postman请求时,我得到两个不同的结果。实际上,邮递员数据是正确的,但无论我添加或删除它不更新的数据,URL会话总是有相同的响应。谁能看一下吗。如果它只发生在我身上,或者后端服务器出现问题 我已经用URLSession.shared和postman测试了请求数据 实际上,我也希望通过URLSession请求获得通过邮递员获得的数据 func getAvailableCoupons(urlString:String, completion: @es

获取方法

当我通过URLSession和Postman请求时,我得到两个不同的结果。实际上,邮递员数据是正确的,但无论我添加或删除它不更新的数据,URL会话总是有相同的响应。谁能看一下吗。如果它只发生在我身上,或者后端服务器出现问题

我已经用URLSession.shared和postman测试了请求数据

实际上,我也希望通过URLSession请求获得通过邮递员获得的数据

   func getAvailableCoupons(urlString:String, completion: @escaping (_ 
    product: Any, _ error: Error?) -> Void){
    guard let url = URL(string: urlString) else {return}
   let task = URLSession.shared.dataTask(with: url) { (data, response, 
 error) in
        let jsonDecoder = JSONDecoder()
        guard let dataResponse = data,
            error == nil else {
                print(error?.localizedDescription ?? "Response Error")
                return }

        let statusCode = (response as! HTTPURLResponse).statusCode

        let responseJSON = try? JSONSerialization.jsonObject(with: dataResponse, options: [])
        if let responseJSON = responseJSON as? [String: Any] {

            if statusCode == 200 {
                do {
                    let jsonData = try JSONSerialization.data(withJSONObject: responseJSON, options: [])
                    let responseData = try jsonDecoder.decode(CoupensResponseModel.self, from:jsonData)
                    completion(responseData, nil)

                } catch let error {
                    print("error when parshing json response \(error)")
                    completion(error, nil )
                }

            } else if statusCode == 404{
                completion(" 404 not found", nil )

            } else {
                print("fatal error \(error?.localizedDescription ?? "big errror")")
            }
        }

    }
    task.resume()



}





  import Foundation

 // MARK: - CoupensResponseModel
 struct CoupensResponseModel: Codable {
 let couponDetails: [CouponDetail]?
   enum CodingKeys: String, CodingKey {
    case couponDetails = "coupon_details"
     }
   }

// MARK: - CouponDetail
 struct CouponDetail: Codable {
let id: Int?
let vouchersusageSet: [VouchersusageSet]?
let couponCode: String?
let minimumSpend: Int?
let expiryDate, createdDate: String?
let discountPrice, discountPercent: Int?
let discountBasis: DiscountBasis?
let couponImage: String?
let couponType: String?

enum CodingKeys: String, CodingKey {
    case id
    case vouchersusageSet = "vouchersusage_set"
    case couponCode = "coupon_code"
    case minimumSpend = "minimum_spend"
    case expiryDate = "expiry_date"
    case createdDate = "created_date"
    case discountPrice = "discount_price"
    case discountPercent = "discount_percent"
    case discountBasis = "discount_basis"
    case couponImage = "coupon_image"
    case couponType = "coupon_type"
     }
  }

   enum DiscountBasis: String, Codable {
case amount = "amount"
case percent = "percent"
      }

   // MARK: - VouchersusageSet
  struct VouchersusageSet: Codable {
let id: Int?
let itemObj: ItemObj?
let voucherObj: Int?
let sectionObj, categoryObj: Int?
         }

    // MARK: - ItemObj
     struct ItemObj: Codable {
  let id: Int?
let code, productName: String?
let productPrice, discountedPrice: Int?
let productDescription, itemImage: String?
let categoryObj: CouponCategoryObj?

   enum CodingKeys: String, CodingKey {
    case id, code
    case productName = "product_name"
    case productPrice = "product_price"
    case discountedPrice = "discounted_price"
    case productDescription = "product_description"
    case itemImage = "item_image"
    case categoryObj
      }
   }

  // MARK: - CouponCategoryObj
  struct CouponCategoryObj: Codable {
let id: Int?
let categoryCode, categoryName: String?
let categoryImage: CouponJSONNull?
let sectionObj: Int?

  enum CodingKeys: String, CodingKey {
    case id
    case categoryCode = "category_code"
    case categoryName = "category_name"
    case categoryImage = "category_image"
    case sectionObj
}
  }
//标记:-编码/解码帮助程序

  class CouponJSONNull: Codable, Hashable {

public static func == (lhs: CouponJSONNull, rhs: CouponJSONNull) -> 
   Bool {
    return true
}

public var hashValue: Int {
    return 0
}

public func hash(into hasher: inout Hasher) {
    // No-op
}
public init() {}

    public required init(from decoder: Decoder) throws {
    let container = try decoder.singleValueContainer()
    if !container.decodeNil() {
        throw DecodingError.typeMismatch(CouponJSONNull.self, 
      DecodingError.Context(codingPath: decoder.codingPath, 
     debugDescription: 
      "Wrong type for CouponJSONNull"))
    }
  }

  public func encode(to encoder: Encoder) throws {
    var container = encoder.singleValueContainer()
    try container.encodeNil()
   }
}

试试下面的方法

let headers = [
    "Cache-Control": "no-cache",
]

let request = NSMutableURLRequest(url: NSURL(string: "http://173.249.20.137:9000/apiapp/coupon")! as URL,
                                  cachePolicy: .useProtocolCachePolicy,
                                  timeoutInterval: 10.0)
request.httpMethod = "GET"
request.allHTTPHeaderFields = headers

let session = URLSession.shared
let dataTask = session.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) -> Void in
    if (error != nil) {
        print(error)
    } else {
        let string = String(data: data!, encoding: .utf8) ?? ""
        print(string)
    }
})
dataTask.resume()

请看回复都是一样的

邮递员回应:

代码响应:

您需要添加代码,以便我们可以查看。虽然您遇到了询问您是否需要响应的问题,但我添加了一些代码,您是否希望我也添加其他代码,您是否尝试了我的答案?是的,我尝试了。但它不起作用。我还添加了数据响应模型。如果这对你有帮助的话,请告诉我你是否发现了任何不同---@Kiran