I am trying to make an http POST request to a local server, but when I make the call, I get a 400 error on the server side, so I believe there is a problem in the way I am making the request in the Swift code. I am not sure if I am setting the JSON body correctly, but any help would be greatly appreciated. Thanks in advance!
swift code:
let url = URL(string: "http://127.0.0.1:5000/get-friends")!
var request = URLRequest(url: url, cachePolicy: .useProtocolCachePolicy, timeoutInterval: 10)
request.httpMethod = "POST"
let tempIdDictionary: [String: Int] = ["user_id": 17]
let jsonBody = try? JSONSerialization.data(withJSONObject: tempIdDictionary, options: .fragmentsAllowed)
request.httpBody = jsonBody
let task = URLSession.shared.dataTask(with: request) { (data, response, error) in
guard let data = data else { return }
do {
let friendList = try JSONDecoder().decode(FriendList.self, from: data)
} catch let jsonErr {
print(jsonErr)
}
}
task.resume()
struct to hold the json data:
struct FriendList: Codable {
let result: [Int]
}
this is the error that shows up in swift when I try to make the request:
dataCorrupted(Swift.DecodingError.Context(codingPath: [], debugDescription: "The given data was not valid JSON.", underlyingError: Optional(Error Domain=NSCocoaErrorDomain Code=3840 "Invalid value around character 0." UserInfo={NSDebugDescription=Invalid value around character 0.})))
Update: From the screenshot that you've added just now it's evident that you need to pass the data as
form-data
instead ofjson
. Here's how you do this, add the following extensions:Then modify the request as follows: