Alamofire POST download PDF
I tried with NSURLSession with no luck : How to download PDF use NSURLSession POST with headers and body parameters?
. So I tried with Alamofire, but always got failure. Here is my code :
func callAPIMoneyReport() {
let param = HTTPBody.moneyReportBody(contactId: contactIdentifier!)
print("param: (String(describing: param))")
let url = NSURL(string:Constant.getAPIMONEYReport())
print("MONEY REPORT URL:(String(describing: url!))")
Alamofire.request(Constant.getAPIMONEYReport(), method: .post, parameters: param, encoding: JSONEncoding.default, headers: ConnectionManager.sharedIns.getHTTPHeadersRefreshToken())
.responseObject{ (response: DataResponse<MainResponse>) in
switch(response.result) {
case .success(_):
print("MONEY report success")
if response.result.value != nil {
self.moneyReportModel = response.result.value
let status = self.moneyReportModel?.status
let statusCode = response.response?.statusCode
print("statusCode:(String(describing: statusCode))")
if(statusCode == 200){
}else{
AlertViewUtil.sharedIns.showAlertFeedback(self, messageText: status!, actionButton1: Wordings.BTN_CLOSE, actionButton2: "")
}
}
break
case .failure(_):
print("MONEY report failure")
break
}
}
}
Update:
Try with this code got success but cannot download PDF:
Alamofire.request(Constant.getAPIMONEYReport(), method: .post, parameters: param, encoding: JSONEncoding.default, headers: ConnectionManager.sharedIns.getHTTPHeadersRefreshToken())
.responseData { response in
print("request:(request)")
print("response:(response.data!)")
}
Update try also with this code, but cannot find the pdf :
let destination: DownloadRequest.DownloadFileDestination = { _, _ in
let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0];
let fileURL = documentsURL.appendingPathComponent("family-money.pdf")
return (fileURL, [.removePreviousFile, .createIntermediateDirectories])
}
Alamofire.download(Constant.getAPIMONEYReport(), method: .post, parameters: param, encoding: JSONEncoding.default, headers: ConnectionManager.sharedIns.getHTTPHeadersRefreshToken(), to: destination)
.response { result in
self.dismissProgressDialog()
if let error = result.error {
print("MONEY report failure")
} else {
print("MONEY report success")
let vc = UIDocumentInteractionController(url: result.destinationURL!)
vc.delegate = self
vc.presentPreview(animated: true)
}
}
What is the correct code for get the PDF?
Update got it, because forget put this code:
vc.name = result.destinationURL!.lastPathComponent
swift pdf post alamofire
add a comment |
I tried with NSURLSession with no luck : How to download PDF use NSURLSession POST with headers and body parameters?
. So I tried with Alamofire, but always got failure. Here is my code :
func callAPIMoneyReport() {
let param = HTTPBody.moneyReportBody(contactId: contactIdentifier!)
print("param: (String(describing: param))")
let url = NSURL(string:Constant.getAPIMONEYReport())
print("MONEY REPORT URL:(String(describing: url!))")
Alamofire.request(Constant.getAPIMONEYReport(), method: .post, parameters: param, encoding: JSONEncoding.default, headers: ConnectionManager.sharedIns.getHTTPHeadersRefreshToken())
.responseObject{ (response: DataResponse<MainResponse>) in
switch(response.result) {
case .success(_):
print("MONEY report success")
if response.result.value != nil {
self.moneyReportModel = response.result.value
let status = self.moneyReportModel?.status
let statusCode = response.response?.statusCode
print("statusCode:(String(describing: statusCode))")
if(statusCode == 200){
}else{
AlertViewUtil.sharedIns.showAlertFeedback(self, messageText: status!, actionButton1: Wordings.BTN_CLOSE, actionButton2: "")
}
}
break
case .failure(_):
print("MONEY report failure")
break
}
}
}
Update:
Try with this code got success but cannot download PDF:
Alamofire.request(Constant.getAPIMONEYReport(), method: .post, parameters: param, encoding: JSONEncoding.default, headers: ConnectionManager.sharedIns.getHTTPHeadersRefreshToken())
.responseData { response in
print("request:(request)")
print("response:(response.data!)")
}
Update try also with this code, but cannot find the pdf :
let destination: DownloadRequest.DownloadFileDestination = { _, _ in
let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0];
let fileURL = documentsURL.appendingPathComponent("family-money.pdf")
return (fileURL, [.removePreviousFile, .createIntermediateDirectories])
}
Alamofire.download(Constant.getAPIMONEYReport(), method: .post, parameters: param, encoding: JSONEncoding.default, headers: ConnectionManager.sharedIns.getHTTPHeadersRefreshToken(), to: destination)
.response { result in
self.dismissProgressDialog()
if let error = result.error {
print("MONEY report failure")
} else {
print("MONEY report success")
let vc = UIDocumentInteractionController(url: result.destinationURL!)
vc.delegate = self
vc.presentPreview(animated: true)
}
}
What is the correct code for get the PDF?
Update got it, because forget put this code:
vc.name = result.destinationURL!.lastPathComponent
swift pdf post alamofire
If your trying to download a PDF in your app, why are you using POST instead of GET?
– swiftcoder
Jan 2 at 4:46
@swiftcoder, because my Client want use POST, more save
– Sarimin
Jan 2 at 5:38
@swiftcoder, and client did not want to populate server with many pdf. So they choose POST because of save and did not populate server with pdf. pdf download direct to user (who click the button)
– Sarimin
Jan 2 at 6:36
add a comment |
I tried with NSURLSession with no luck : How to download PDF use NSURLSession POST with headers and body parameters?
. So I tried with Alamofire, but always got failure. Here is my code :
func callAPIMoneyReport() {
let param = HTTPBody.moneyReportBody(contactId: contactIdentifier!)
print("param: (String(describing: param))")
let url = NSURL(string:Constant.getAPIMONEYReport())
print("MONEY REPORT URL:(String(describing: url!))")
Alamofire.request(Constant.getAPIMONEYReport(), method: .post, parameters: param, encoding: JSONEncoding.default, headers: ConnectionManager.sharedIns.getHTTPHeadersRefreshToken())
.responseObject{ (response: DataResponse<MainResponse>) in
switch(response.result) {
case .success(_):
print("MONEY report success")
if response.result.value != nil {
self.moneyReportModel = response.result.value
let status = self.moneyReportModel?.status
let statusCode = response.response?.statusCode
print("statusCode:(String(describing: statusCode))")
if(statusCode == 200){
}else{
AlertViewUtil.sharedIns.showAlertFeedback(self, messageText: status!, actionButton1: Wordings.BTN_CLOSE, actionButton2: "")
}
}
break
case .failure(_):
print("MONEY report failure")
break
}
}
}
Update:
Try with this code got success but cannot download PDF:
Alamofire.request(Constant.getAPIMONEYReport(), method: .post, parameters: param, encoding: JSONEncoding.default, headers: ConnectionManager.sharedIns.getHTTPHeadersRefreshToken())
.responseData { response in
print("request:(request)")
print("response:(response.data!)")
}
Update try also with this code, but cannot find the pdf :
let destination: DownloadRequest.DownloadFileDestination = { _, _ in
let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0];
let fileURL = documentsURL.appendingPathComponent("family-money.pdf")
return (fileURL, [.removePreviousFile, .createIntermediateDirectories])
}
Alamofire.download(Constant.getAPIMONEYReport(), method: .post, parameters: param, encoding: JSONEncoding.default, headers: ConnectionManager.sharedIns.getHTTPHeadersRefreshToken(), to: destination)
.response { result in
self.dismissProgressDialog()
if let error = result.error {
print("MONEY report failure")
} else {
print("MONEY report success")
let vc = UIDocumentInteractionController(url: result.destinationURL!)
vc.delegate = self
vc.presentPreview(animated: true)
}
}
What is the correct code for get the PDF?
Update got it, because forget put this code:
vc.name = result.destinationURL!.lastPathComponent
swift pdf post alamofire
I tried with NSURLSession with no luck : How to download PDF use NSURLSession POST with headers and body parameters?
. So I tried with Alamofire, but always got failure. Here is my code :
func callAPIMoneyReport() {
let param = HTTPBody.moneyReportBody(contactId: contactIdentifier!)
print("param: (String(describing: param))")
let url = NSURL(string:Constant.getAPIMONEYReport())
print("MONEY REPORT URL:(String(describing: url!))")
Alamofire.request(Constant.getAPIMONEYReport(), method: .post, parameters: param, encoding: JSONEncoding.default, headers: ConnectionManager.sharedIns.getHTTPHeadersRefreshToken())
.responseObject{ (response: DataResponse<MainResponse>) in
switch(response.result) {
case .success(_):
print("MONEY report success")
if response.result.value != nil {
self.moneyReportModel = response.result.value
let status = self.moneyReportModel?.status
let statusCode = response.response?.statusCode
print("statusCode:(String(describing: statusCode))")
if(statusCode == 200){
}else{
AlertViewUtil.sharedIns.showAlertFeedback(self, messageText: status!, actionButton1: Wordings.BTN_CLOSE, actionButton2: "")
}
}
break
case .failure(_):
print("MONEY report failure")
break
}
}
}
Update:
Try with this code got success but cannot download PDF:
Alamofire.request(Constant.getAPIMONEYReport(), method: .post, parameters: param, encoding: JSONEncoding.default, headers: ConnectionManager.sharedIns.getHTTPHeadersRefreshToken())
.responseData { response in
print("request:(request)")
print("response:(response.data!)")
}
Update try also with this code, but cannot find the pdf :
let destination: DownloadRequest.DownloadFileDestination = { _, _ in
let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0];
let fileURL = documentsURL.appendingPathComponent("family-money.pdf")
return (fileURL, [.removePreviousFile, .createIntermediateDirectories])
}
Alamofire.download(Constant.getAPIMONEYReport(), method: .post, parameters: param, encoding: JSONEncoding.default, headers: ConnectionManager.sharedIns.getHTTPHeadersRefreshToken(), to: destination)
.response { result in
self.dismissProgressDialog()
if let error = result.error {
print("MONEY report failure")
} else {
print("MONEY report success")
let vc = UIDocumentInteractionController(url: result.destinationURL!)
vc.delegate = self
vc.presentPreview(animated: true)
}
}
What is the correct code for get the PDF?
Update got it, because forget put this code:
vc.name = result.destinationURL!.lastPathComponent
swift pdf post alamofire
swift pdf post alamofire
edited Jan 2 at 7:04
Sarimin
asked Jan 2 at 3:33
SariminSarimin
3171311
3171311
If your trying to download a PDF in your app, why are you using POST instead of GET?
– swiftcoder
Jan 2 at 4:46
@swiftcoder, because my Client want use POST, more save
– Sarimin
Jan 2 at 5:38
@swiftcoder, and client did not want to populate server with many pdf. So they choose POST because of save and did not populate server with pdf. pdf download direct to user (who click the button)
– Sarimin
Jan 2 at 6:36
add a comment |
If your trying to download a PDF in your app, why are you using POST instead of GET?
– swiftcoder
Jan 2 at 4:46
@swiftcoder, because my Client want use POST, more save
– Sarimin
Jan 2 at 5:38
@swiftcoder, and client did not want to populate server with many pdf. So they choose POST because of save and did not populate server with pdf. pdf download direct to user (who click the button)
– Sarimin
Jan 2 at 6:36
If your trying to download a PDF in your app, why are you using POST instead of GET?
– swiftcoder
Jan 2 at 4:46
If your trying to download a PDF in your app, why are you using POST instead of GET?
– swiftcoder
Jan 2 at 4:46
@swiftcoder, because my Client want use POST, more save
– Sarimin
Jan 2 at 5:38
@swiftcoder, because my Client want use POST, more save
– Sarimin
Jan 2 at 5:38
@swiftcoder, and client did not want to populate server with many pdf. So they choose POST because of save and did not populate server with pdf. pdf download direct to user (who click the button)
– Sarimin
Jan 2 at 6:36
@swiftcoder, and client did not want to populate server with many pdf. So they choose POST because of save and did not populate server with pdf. pdf download direct to user (who click the button)
– Sarimin
Jan 2 at 6:36
add a comment |
0
active
oldest
votes
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',
autoActivateHeartbeat: false,
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
});
}
});
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%2f54000924%2falamofire-post-download-pdf%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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%2f54000924%2falamofire-post-download-pdf%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
If your trying to download a PDF in your app, why are you using POST instead of GET?
– swiftcoder
Jan 2 at 4:46
@swiftcoder, because my Client want use POST, more save
– Sarimin
Jan 2 at 5:38
@swiftcoder, and client did not want to populate server with many pdf. So they choose POST because of save and did not populate server with pdf. pdf download direct to user (who click the button)
– Sarimin
Jan 2 at 6:36