The Espionage Act was passed in 1917 with two objectives: cracking down on internal dissent created by immigrants, and preventing sensitive military information from reaching potential enemies. President Wilson had wanted such an act since 1915, but Congress refused to enact what many considered to be an infringement upon first amendment rights and an overreach of power by the president and the federal government more generally.
Later, in 1918, using wartime hysteria to his advantage, Wilson pushed the passage of the Sedition Act, which "forbade the use of "disloyal, profane, scurrilous, or abusive language" about the United States government, its flag, or its armed forces or that caused others to view the American government or its institutions with contempt", again threatening free speech and ending up being declared unconstitutional in 1921.
Hope this helps!