Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add POST endpoints to resolve browser error about GET request wi… #930

Merged
merged 1 commit into from
Jan 26, 2025

Conversation

sd0ric4
Copy link
Contributor

@sd0ric4 sd0ric4 commented Jan 24, 2025

Title: Add POST endpoints for browser compatibility with file uploads

Description:
This PR adds POST endpoints for routes that handle file uploads, while maintaining the existing GET endpoints for compatibility. The change is necessary because:

  1. Browser Limitations:
  • Browsers strictly enforce HTTP specifications which prohibit GET requests from having a request body
  • When uploading files through FormData, browsers will throw the error: "Failed to execute 'fetch' on 'Window': Request with GET/HEAD method cannot have body"
  1. Technical Context:
  • File uploads and form data must be sent in the request body
  • While the backend can handle file data in GET requests, browsers cannot send such requests
  • Adding POST endpoints allows browsers to properly upload files while maintaining backward compatibility
  1. Changes Made:
  • Added POST endpoints for routes handling file uploads
  • Maintained existing GET endpoints for API compatibility
  • No changes to the underlying business logic

This modification ensures proper browser support while maintaining API compatibility.

@sd0ric4
Copy link
Contributor Author

sd0ric4 commented Jan 24, 2025

Error on browser

image image

The Code I wrote for testing API

  const handleSubmit = async (e: FormEvent<HTMLFormElement>) => {
    e.preventDefault();
    setIsLoading(true);
    setError('');

    try {
      const formData = new FormData();
      formData.append('tts_text', ttsText);
      formData.append('prompt_text', promptText);
      if (promptFile) {
        formData.append('prompt_wav', promptFile);
      }

      console.log('Sending request with data:', {
        ttsText,
        promptText,
        promptFile: promptFile?.name,
      });

      const response = await fetch(
        'http://192.168.0.118:50002/inference_zero_shot',
        {
          method: 'GET',
          body: formData,
        }
      );

      console.log('Response status:', response.status);

      const contentType = response.headers.get('Content-Type');
      console.log('Response content type:', contentType);

      if (!response.ok) {
        throw new Error('Failed to generate audio');
      }

      const audioBlob = await response.blob();
      const audioUrl = URL.createObjectURL(audioBlob);

      if (audioRef.current) {
        audioRef.current.src = audioUrl;
        await audioRef.current.play();
      }
    } catch (error: unknown) {
      console.error('Error:', error);
      setError(
        error instanceof Error ? error.message : 'An unknown error occurred'
      );
    } finally {
      setIsLoading(false);
    }
  };

@sd0ric4 sd0ric4 changed the title fix: add POST endpoints to resolve browser error about GET request wi… feat: add POST endpoints to resolve browser error about GET request wi… Jan 24, 2025
@sd0ric4
Copy link
Contributor Author

sd0ric4 commented Jan 25, 2025

Would it be okay if I submit a PR to refactor the FastAPI implementation?

@aluminumbox aluminumbox merged commit 86e26f5 into FunAudioLLM:main Jan 26, 2025
syaofox pushed a commit to syaofox/CosyVoice that referenced this pull request Mar 4, 2025
feat: add POST endpoints to resolve browser error about GET request wi…
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants