0

Hello so I have a Django API whereby I have the following in my DB:

  • lecturer
  • module
  • video
  • date

So when I make a POST request to create another entry to my DB with POSTMAN everything works just fine leaving me to believe that my API is definitely handling everything properly.

So now when I try to make a POST request on my client-side when I submit the data I get the following error: [*] Error: {'video': [ErrorDetail(string='The submitted data was not a file. Check the encoding type on the form.', code='invalid')]}

So I honestly do not know where I could be going wrong with how I am handling this request can I please get assistance with regards to this matter.

Code below: ReactJS code how I tried to handle this POST request

class VideoUploadForm extends Component{

    constructor(props){
        super(props);

        this.state = {

            lecturer: '',
            module: '',
            video: null,
            date: ''
        }

        this.handleChange = this.handleChange.bind(this);
        this.uploadVideo = this.uploadVideo.bind(this);
    }


    handleChange = (e) =>{

        this.setState({

            [e.target.id] : e.target.value
        });
    }




    uploadVideo = (e) => {

        e.preventDefault();
        let form_data = new FormData();
        form_data.append('lecturer', this.state.lecturer);
        form_data.append('module', this.state.module);
        form_data.append('video', this.state.video);
        form_data.append('date', this.state.date);
        let url = 'http://127.0.0.1:8000/api/upload-lecture/';
        axios.post(url, form_data, {
          headers: {
        
            'Accept':'application/json',
            'content-type': 'application/json'
          }
        })
            .then(res => {
              console.log(res.data);
           })
            .catch(err => console.log(err))
    };





    render(){

        return(

            <div className="form__container">
            
                <form className="form__items" onSubmit={this.uploadVideo}>

                    <label>
                        Lecturer:
                        <input type="text" id="lecturer" onChange={this.handleChange} required/>  
                    </label>

                    <label>
                        Module: 
                        <input type="text" id="module" onChange={this.handleChange}  required/>  
                    </label>

                    <label>
                        Video: 
                        <input type="file" id="video" accept="video/*" onChange={this.handleChange} required/>  
                    </label>

                    <label>
                        Date: 
                        <input type="text" onChange={this.handleChange} required/>  
                    </label>

                    <input type="submit" value="Upload Video" onSubmit={this.uploadVideo} required/>
            
            
                </form>
            
        
            </div>
        )
    }
}

export default VideoUploadForm;


@api_view(['POST'])
def videoUpload(request):

    parser_classes = (JSONParser, FormParser, MultiPartParser)
    serializer = LectureVideosSerializer(data=request.data)

    if serializer.is_valid():
        serializer.save()
        return Response(serializer.data)

    else:
        print("[*] Error: " + str(serializer.errors))
    return Response(serializer.data)

What am I am doing wrong with this implementation here of making a POST request?

I have checked one similar Question on stackoverflow but when I tried to implement the solution that was given and worked for the other person to me my errors were still consitant never changed

  • Can you remove content-type and share with us if there is any error – Ubeyt Demir Aug 18 '20 at 09:19
  • Does this answer your question? [How to post a file from a form with Axios](https://stackoverflow.com/questions/43013858/how-to-post-a-file-from-a-form-with-axios) – sibasishm Aug 18 '20 at 09:21
  • @UbeytDemir Yes, I still get the same error message from the API logs. –  Aug 18 '20 at 09:26
  • @SibasishMohanty let me check that quick –  Aug 18 '20 at 09:26
  • How you are parsing request body at the backend ? – Ubeyt Demir Aug 18 '20 at 09:29
  • Let grab the code and add it here in the question\ –  Aug 18 '20 at 09:31
  • did you change the content type to 'multipart/form-data'? Cause as it is right now 'application/json' will be parsed using JSONParser instead of form parser at the backend. – Bhagwan Thapa Aug 18 '20 at 10:08
  • Yes this is what I have in my header now coming from the suggested answer ```headers:{'Accept':'application/json', 'Content-Type': 'multipart/form-data'}``` but still getting the same error –  Aug 18 '20 at 10:14
  • i don't think `'Accept':'application/json'` is necessary – Linh Nguyen Aug 18 '20 at 10:22

0 Answers0