Alamofire unacceptable content type json mysql
up vote
0
down vote
favorite
I am posting data to a mysql database using alamofire and I can see the data appearing in the database so the code is working. However, I don't like the error or notice I am seeing in Xcode console which says:
responseValidationFailed(reason: Alamofire.AFError.ResponseValidationFailureReason.unacceptableContentType(acceptableContentTypes: ["application/json"], responseContentType: "text/html"))
I am not that familiar with Alamofire but it looks like the error is saying I am posting text/html and not json. But this isn't what I want, I want to post json. I am not sure if I need to use codable and encode the data first or what the issue is if there is actually an issue. Because my other thought is that .responseJSON is actually converting the parameters into json format and that's why the validation gives me that error because it is checking the data before it is formatted as json?
let url = URL(string: "http://localhost:8888/mobile/bd_booking.php")
let parameters: Parameters = [
"firstName": namesTxt.text,
"email": emailTxt.text,
"contactNo": contactNoTxt.text
]
Alamofire.request(url!, method: .post, parameters: parameters)
.validate(statusCode: 200..<300)
.validate(contentType: ["application/json"])
.responseJSON { response in
switch response.result {
case .success:
print("Success")
case .failure(let error):
print(error)
}
}
swift alamofire
add a comment |
up vote
0
down vote
favorite
I am posting data to a mysql database using alamofire and I can see the data appearing in the database so the code is working. However, I don't like the error or notice I am seeing in Xcode console which says:
responseValidationFailed(reason: Alamofire.AFError.ResponseValidationFailureReason.unacceptableContentType(acceptableContentTypes: ["application/json"], responseContentType: "text/html"))
I am not that familiar with Alamofire but it looks like the error is saying I am posting text/html and not json. But this isn't what I want, I want to post json. I am not sure if I need to use codable and encode the data first or what the issue is if there is actually an issue. Because my other thought is that .responseJSON is actually converting the parameters into json format and that's why the validation gives me that error because it is checking the data before it is formatted as json?
let url = URL(string: "http://localhost:8888/mobile/bd_booking.php")
let parameters: Parameters = [
"firstName": namesTxt.text,
"email": emailTxt.text,
"contactNo": contactNoTxt.text
]
Alamofire.request(url!, method: .post, parameters: parameters)
.validate(statusCode: 200..<300)
.validate(contentType: ["application/json"])
.responseJSON { response in
switch response.result {
case .success:
print("Success")
case .failure(let error):
print(error)
}
}
swift alamofire
You probably have to set theContent-Type
toapplication/json
in your request. For instance usingAlamofire.request(request)
constructor, and parametrise the request beforehand :request.setValue("application/json", forHTTPHeaderField: "Content-Type")
.
– Olympiloutre
yesterday
My bad it seems you have an error related on theresponse
and not therequest
. Nevermind. Something you could try is to get rid of the.validate(contentType: ["application/json"])
line, and print theresponse
in the completion handler. You'll see the full answer that way
– Olympiloutre
yesterday
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I am posting data to a mysql database using alamofire and I can see the data appearing in the database so the code is working. However, I don't like the error or notice I am seeing in Xcode console which says:
responseValidationFailed(reason: Alamofire.AFError.ResponseValidationFailureReason.unacceptableContentType(acceptableContentTypes: ["application/json"], responseContentType: "text/html"))
I am not that familiar with Alamofire but it looks like the error is saying I am posting text/html and not json. But this isn't what I want, I want to post json. I am not sure if I need to use codable and encode the data first or what the issue is if there is actually an issue. Because my other thought is that .responseJSON is actually converting the parameters into json format and that's why the validation gives me that error because it is checking the data before it is formatted as json?
let url = URL(string: "http://localhost:8888/mobile/bd_booking.php")
let parameters: Parameters = [
"firstName": namesTxt.text,
"email": emailTxt.text,
"contactNo": contactNoTxt.text
]
Alamofire.request(url!, method: .post, parameters: parameters)
.validate(statusCode: 200..<300)
.validate(contentType: ["application/json"])
.responseJSON { response in
switch response.result {
case .success:
print("Success")
case .failure(let error):
print(error)
}
}
swift alamofire
I am posting data to a mysql database using alamofire and I can see the data appearing in the database so the code is working. However, I don't like the error or notice I am seeing in Xcode console which says:
responseValidationFailed(reason: Alamofire.AFError.ResponseValidationFailureReason.unacceptableContentType(acceptableContentTypes: ["application/json"], responseContentType: "text/html"))
I am not that familiar with Alamofire but it looks like the error is saying I am posting text/html and not json. But this isn't what I want, I want to post json. I am not sure if I need to use codable and encode the data first or what the issue is if there is actually an issue. Because my other thought is that .responseJSON is actually converting the parameters into json format and that's why the validation gives me that error because it is checking the data before it is formatted as json?
let url = URL(string: "http://localhost:8888/mobile/bd_booking.php")
let parameters: Parameters = [
"firstName": namesTxt.text,
"email": emailTxt.text,
"contactNo": contactNoTxt.text
]
Alamofire.request(url!, method: .post, parameters: parameters)
.validate(statusCode: 200..<300)
.validate(contentType: ["application/json"])
.responseJSON { response in
switch response.result {
case .success:
print("Success")
case .failure(let error):
print(error)
}
}
swift alamofire
swift alamofire
edited yesterday
asked yesterday
user8463989
11810
11810
You probably have to set theContent-Type
toapplication/json
in your request. For instance usingAlamofire.request(request)
constructor, and parametrise the request beforehand :request.setValue("application/json", forHTTPHeaderField: "Content-Type")
.
– Olympiloutre
yesterday
My bad it seems you have an error related on theresponse
and not therequest
. Nevermind. Something you could try is to get rid of the.validate(contentType: ["application/json"])
line, and print theresponse
in the completion handler. You'll see the full answer that way
– Olympiloutre
yesterday
add a comment |
You probably have to set theContent-Type
toapplication/json
in your request. For instance usingAlamofire.request(request)
constructor, and parametrise the request beforehand :request.setValue("application/json", forHTTPHeaderField: "Content-Type")
.
– Olympiloutre
yesterday
My bad it seems you have an error related on theresponse
and not therequest
. Nevermind. Something you could try is to get rid of the.validate(contentType: ["application/json"])
line, and print theresponse
in the completion handler. You'll see the full answer that way
– Olympiloutre
yesterday
You probably have to set the
Content-Type
to application/json
in your request. For instance using Alamofire.request(request)
constructor, and parametrise the request beforehand : request.setValue("application/json", forHTTPHeaderField: "Content-Type")
.– Olympiloutre
yesterday
You probably have to set the
Content-Type
to application/json
in your request. For instance using Alamofire.request(request)
constructor, and parametrise the request beforehand : request.setValue("application/json", forHTTPHeaderField: "Content-Type")
.– Olympiloutre
yesterday
My bad it seems you have an error related on the
response
and not the request
. Nevermind. Something you could try is to get rid of the .validate(contentType: ["application/json"])
line, and print the response
in the completion handler. You'll see the full answer that way– Olympiloutre
yesterday
My bad it seems you have an error related on the
response
and not the request
. Nevermind. Something you could try is to get rid of the .validate(contentType: ["application/json"])
line, and print the response
in the completion handler. You'll see the full answer that way– Olympiloutre
yesterday
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
Recently, I had a similar issue.
I resolved the error by reading this official section:
https://github.com/Alamofire/Alamofire/blob/master/Documentation/Usage.md#json-encoding
We need to append encoding parameters too while making the request.
For example:
I hope you'll be able to resolve the current issue by implementing this.
Thank you, I tried that but am still getting the same error
– user8463989
yesterday
Could you please share the response format again?
– Sikandar Khan
yesterday
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
Recently, I had a similar issue.
I resolved the error by reading this official section:
https://github.com/Alamofire/Alamofire/blob/master/Documentation/Usage.md#json-encoding
We need to append encoding parameters too while making the request.
For example:
I hope you'll be able to resolve the current issue by implementing this.
Thank you, I tried that but am still getting the same error
– user8463989
yesterday
Could you please share the response format again?
– Sikandar Khan
yesterday
add a comment |
up vote
0
down vote
Recently, I had a similar issue.
I resolved the error by reading this official section:
https://github.com/Alamofire/Alamofire/blob/master/Documentation/Usage.md#json-encoding
We need to append encoding parameters too while making the request.
For example:
I hope you'll be able to resolve the current issue by implementing this.
Thank you, I tried that but am still getting the same error
– user8463989
yesterday
Could you please share the response format again?
– Sikandar Khan
yesterday
add a comment |
up vote
0
down vote
up vote
0
down vote
Recently, I had a similar issue.
I resolved the error by reading this official section:
https://github.com/Alamofire/Alamofire/blob/master/Documentation/Usage.md#json-encoding
We need to append encoding parameters too while making the request.
For example:
I hope you'll be able to resolve the current issue by implementing this.
Recently, I had a similar issue.
I resolved the error by reading this official section:
https://github.com/Alamofire/Alamofire/blob/master/Documentation/Usage.md#json-encoding
We need to append encoding parameters too while making the request.
For example:
I hope you'll be able to resolve the current issue by implementing this.
answered yesterday
Sikandar Khan
4911
4911
Thank you, I tried that but am still getting the same error
– user8463989
yesterday
Could you please share the response format again?
– Sikandar Khan
yesterday
add a comment |
Thank you, I tried that but am still getting the same error
– user8463989
yesterday
Could you please share the response format again?
– Sikandar Khan
yesterday
Thank you, I tried that but am still getting the same error
– user8463989
yesterday
Thank you, I tried that but am still getting the same error
– user8463989
yesterday
Could you please share the response format again?
– Sikandar Khan
yesterday
Could you please share the response format again?
– Sikandar Khan
yesterday
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53371381%2falamofire-unacceptable-content-type-json-mysql%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
You probably have to set the
Content-Type
toapplication/json
in your request. For instance usingAlamofire.request(request)
constructor, and parametrise the request beforehand :request.setValue("application/json", forHTTPHeaderField: "Content-Type")
.– Olympiloutre
yesterday
My bad it seems you have an error related on the
response
and not therequest
. Nevermind. Something you could try is to get rid of the.validate(contentType: ["application/json"])
line, and print theresponse
in the completion handler. You'll see the full answer that way– Olympiloutre
yesterday