简体   繁体   中英

post request with body Alamofire

I am trying to post some data to the API, but I keep getting Response status code was unacceptable: 404 error message.. I have checked, the POST parameters and API Url are all correct, example request works on the postman but it is not working on Xcode ..

my POST request Url is in this format: {{API}}/postData/Id/block .

my POST request function with Alamofire is as follows:

   func postData(token: String, id: String, category: String, completion: @escaping(_ data: DataTobePost) -> Void) {
        let header: HTTPHeaders = ["authorization": token]
        let parameter: Parameters = [
            "Id": id,
            "category": category ]
        
        Alamofire.request(API_Configurator.postData, method: .post, parameters: parameter, encoding: JSONEncoding.default, headers: header).validate().responseData(completionHandler: { response in
            switch response.result {
            case .success(let val):
                do {
                    let data = try JSONDecoder().decode(DataTobePost.self, from: val)
                    completion(data)
                }catch {
                    print("[DataTobePost Catch Error while decoding response: \(error.localizedDescription)]")

                }
            case .failure(let error):
                print("[DataTobePost Failure Error : \(error.localizedDescription)]")
            }
        })
    }

and the response is:

{
    "success": true
}

where am i going wrong, can anyone help through this. (I am quite new to Alamofire )

There is no way to check what is wrong. If you got the 404 error it means 2 things:

  1. Code was written correctly(it compiles)
  2. Requested page does not exist (404 error)

I think you need to check your API_Configurator.postData. Usually, it's something simple like extra characters like "//", " ", "." etc.

Or the problem with API. The best way to check API uses Postman

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM