A former U.S. Army soldier who was court martialed after he left his post in Afghanistan and was captured by the Taliban is asking a federal judge to overturn his military conviction, saying his trial was unduly influenced when former President Donald Trump repeatedly made disparaging comments about him and called for his execution.

Bowe Bergdahl filed the complaint in federal court in Washington, D.C. last month, asking a judge to overturn his court martial conviction. Bergdahl says Trump's statements and actions by the late U.S. Sen. John McCain and his military judge violated his Fifth Amendment right to a fair trial. 

"The scandalous meddling in a specific case by leaders of the political branches — one of whom was Commander in Chief of the armed forces — would never be tolerated if the proceeding had been a criminal prosecution in this or any other federal district court and should not be tolerated in a court-martial," Bergdahl's attorneys wrote in the court filing.

ARMY'S REVISED GROOMING STANDARDS LOOSEN UP HAIRSTYLE REGULATIONS, ALLOW FOR EARRINGS

Bergdahl was charged with desertion and misbehavior before the enemy after the then-23-year-old from Hailey, Idaho, left his his post in Afghanistan in 2009. The soldier maintained he was trying to get outside his post so he could report what he saw as poor leadership within his unit, but he was abducted by the Taliban and held captive for nearly five years.

FILE - In this Nov. 3, 2017, file photo, Army Sgt. Bowe Bergdahl leaves the Fort Bragg courtroom facility during deliberations at a sentencing hearing in Fort Bragg, N.C. (AP Photo/Gerry Broome, File)

FILE - In this Nov. 3, 2017, file photo, Army Sgt. Bowe Bergdahl leaves the Fort Bragg courtroom facility during deliberations at a sentencing hearing in Fort Bragg, N.C. (AP Photo/Gerry Broome, File) ((AP Photo/Gerry Broome, File))

During that time, Bergdahl was repeatedly beaten and tortured and he became sick from the squalid conditions. After several escape attempts he was caged and shackled to a metal bed frame, according to court documents.

Other U.S. troops took part in a lengthy search for Bergdahl, and several sustained serious wounds during the search efforts. Bergdahl was ultimately returned as part of a prisoner swap negotiated by then-President Barack Obama in 2014.

US SOLDIER CHARGED AFTER VETERAN WIFE FOUND DEAD IN BARRACKS

The prisoner swap was criticized by Republicans, and Donald Trump made it a talking point during his presidential campaign. He repeatedly called Bergdahl a "dirty, rotten traitor" during campaign rallies and other events, saying the soldier should be executed or left to die in the desert. At times Trump pretended to shoot an imaginary gun when referring to Bergdahl.

McCain also criticized the prisoner exchange deal, saying he applauded Bergdahl's return home but that the swap put American servicemen and women at risk.

In his appeal, Bergdahl said McCain intensely pressured the Army to charge him, calling Bergdahl a "deserter" and threatening to hold a hearing in the Senate Armed Services Committee if Bergdahl went unpunished.

Bergdahl also contends his judge at the court-martial, Judge Jeffrey Nance, failed to disclose that he was applying for a lucrative federal job as an immigration attorney during the case. Bergdahl contends Nance said he was "completely unaffected by any opinions President Trump may have," because he had no hope or ambition for a promotion. But around that same time, Nance was appointed by then-Attorney General Jeff Sessions — a member of Trump's Cabinet — to the immigration judge post, effective the following year.

BLACK SOLDIER MADE HISTORY AS FIRST AFRICAN AMERICAN SNIPER TO DEPLOY WITH 3RD RANGER BATTALION

At the end of his court-martial, Bergdahl was dishonorably discharged, demoted in rank from sergeant to private and ordered to forfeit $10,000 in pay. The dishonorable discharge means that Bergdahl — who was granted a waiver to join the Army because he had mental health problems — can't get military health benefits through the Department of Veterans Affairs.

CLICK HERE TO GET THE FOX NEWS APP

The psychological and physical torture that Bergdahl experienced during his years in captivity exacerbated his pre-existing mental conditions, Bergdahl said, which means he requires complicated and extended medical treatment.

Bergdahl says the allegedly undue influence means his conviction and sentence were given in violation of his Fifth Amendment right to a fair trial. He wants a federal judge to overturn his conviction and sentence and expunge it from his record, restoring his military rights. 

Federal attorneys have not yet responded to the complaint.