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)
}
}









share|improve this question
























  • 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

















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)
}
}









share|improve this question
























  • 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















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)
}
}









share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited yesterday

























asked yesterday









user8463989

11810




11810












  • 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




















  • 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


















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














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:
enter image description here



I hope you'll be able to resolve the current issue by implementing this.






share|improve this answer





















  • 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











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















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

























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:
enter image description here



I hope you'll be able to resolve the current issue by implementing this.






share|improve this answer





















  • 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















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:
enter image description here



I hope you'll be able to resolve the current issue by implementing this.






share|improve this answer





















  • 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













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:
enter image description here



I hope you'll be able to resolve the current issue by implementing this.






share|improve this answer












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:
enter image description here



I hope you'll be able to resolve the current issue by implementing this.







share|improve this answer












share|improve this answer



share|improve this answer










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


















  • 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


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














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





















































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







Popular posts from this blog

404 Error Contact Form 7 ajax form submitting

How to know if a Active Directory user can login interactively

TypeError: fit_transform() missing 1 required positional argument: 'X'