I'm building a little app that talks to the jsonbin API with Alamofire. Their API docs cover using Python to "put" or "update" the JSON file data extensively in Python. I'm working on doing that in Swift 5.
My problem is when I attempt to use a put command to send data to a specific file. See code:
import AlamoFire
func sendData()
{
let headers: HTTPHeaders = [
"Content-Type": "application/json",
"X-Master-Key": "x-x-x-x-x-x-x-x",
"X-Bin-Meta": "false"
]
let queue = DispatchQueue(label: "com.app.name", qos: .background, attributes: .concurrent)
do{
var testData = [
"title": "My Test Task",
"date": "My Date",
"complete": "Completion Status",
"assignee": "Bob Jones"
]
AF.request("https://api.jsonbin.io/v3/b/x-x-x-x-x-x-x", method: .put, parameters: testData, headers: headers).validate().responseJSON(queue: queue) { response in
switch response.result {
case .success(_):
print("Success!")
case .failure(let error):
print(error)
}
}
}
catch
{
print("Error occured.")
}
}
I'm trying to send a simple JSON structure over. The error I receive is this:
esponseValidationFailed(reason: Alamofire.AFError.ResponseValidationFailureReason.unacceptableStatusCode(code: 400))
Their API docs say that an error 400 typically means:
You need to pass Content-Type set to application/json
Invalid Bin Id provided
Bin (JSON Data) cannot be blank
I have verified all of these requirements are correct.
Here is functioning Python code as per their docs:
import requests
url = 'https://api.jsonbin.io/v3/b/x-x-x-x-x-x'
headers = {
'Content-Type': 'application/json',
'X-Master-Key': 'x-x-x-x-x-x-x'
}
data = {"sample": "Hello World"}
req = requests.put(url, json=data, headers=headers)
print(req.text)
Is there something I'm missing? Are the data types different? Is the testData Swift variable different than the data Python variable? How might I go about "putting" data to the RESTful API with Swift and Alamofire 5?
Thanks!
Your current parameters are not sent as JSON, but url encoded.
You can use the cURL description tool method of Alamofire to see how it's sent:
And you'd get:
The important part being
So you need to tell to send the parameters as JSON:
And then, the cURL output parameter line is:
Unrelated to your code, but: There is no need for a
do/catch, there is notry, and it would be recommended to print theerrorif there is really antry, not justprint("Error occured."). butprint("Error occurred: \(error)").Also,
responseJSON()is deprecated, in favor ofCodableandresponseDecodable(of:). If you still want to useJSONSerialization(which is innerly called inresponseJSON()), useresponseData()and call it yourself.