2017-11-30 3 views
1

Alamofire를 사용하여 PUT 요청을하려고하는데 정수가 필요합니다 (개체가 아님). 요청은 데이터베이스에 ID를 보내고 쿼리는 데이터베이스에서 해당 ID를 가진 개체를 업데이트합니다.Alamofire PUT 요청의 매개 변수로 정수 전달

var parameters: Parameters = ["key" : "value"] 

단지 객체를 사용하지 않고 정수를 보낼 수있는 방법이있다 : 는 alamofire의 매개 변수 객체는 객체를 취할 것? 나는 위의이 방법을 사용하면 점점 계속 오류는 다음과 같습니다

nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of int out of START_OBJECT token 

나는 이것이 내가 대신 int를 통과해야합니다 때이 객체를 전달하고 의미 가정입니다. 나는 온라인으로 우리와 함께해야 할 예제를 찾을 수 없습니다

Alamofire.request(url, method: .put, parameters: parameters, encoding: JSONEncoding.default, headers: nil).response{ response in 
     if response.response?.statusCode == 200 { 
      // pass 
     }else{ 
      // fail 
     } 
     completionHandler((response.response?.statusCode)!) 
    } 

:

내 요청입니다.

답변

2

요청을 보내는 위치에 대한 자세한 정보를 제공하면 내 솔루션이 작동하는지 테스트 할 수 있습니다. 그러나 당신은 이것을 시도 할 수 있습니다.

let url = "YOUR_URL" 

let yourData = "WHATEVER CUSTOM VAL YOU WANT TO PASS" 

var request = URLRequest(url: URL(string: url)!) 
request.httpMethod = "PUT" 
//request.setValue("application/json", forHTTPHeaderField: "Content-Type") //This is if you want to have headers. But it doesn't look like you need them. 

request.httpBody = yourData 
/* 
do { 
    request.httpBody = try JSONSerialization.data(withJSONObject: yourData) 
} catch { 
    print("JSON serialization failed: \(error)") 
} 
*/ 
Alamofire.request(request).responseJSON {(response) in 
    if response.response?.statusCode == 200 { 
     print ("pass") 
    }else{ 
     print ("fail") 
    } 
    completionHandler((response.response?.statusCode)!) 

    /* 
    switch response.result { 
    case .success(let value): 
     print ("success: \(value)") 
    case .failure(let error): 
     print("error: \(error)") 
    } 
    */ 
} 
+0

그에게 로컬 호스트 데시벨,하지만 난이 시도하고 나는이 오류가있어 : '*** + [NSJSONSerialization dataWithJSONObject : 옵션 : 오류 :] : 인해 캐치되지 않는 예외'NSInvalidArgumentException ', 이유에 종료 응용 프로그램을 잘못된 최고 JSON의 '-level type'에 ' – Aria

+0

'이라고 쓰면 실제로 테스트 할 때 더 많은 정보 나 API없이 실제로 수행중인 작업을 테스트하기가 어렵습니다. 그러나 방금 변경 한 것을 시도하십시오. –

+0

좋아요, 더 많은 설명을 추가했습니다. 도움이 될 수 있다면. 또한, 보내는 데이터는 문자열이 아닌 정수입니다. 나도 이걸 시도했지만 httpBody는 int로 데이터를 받아들이지 않는 것 같다. 그냥 문자열 – Aria