cURL - file upload with file path location not working with windows command prompt

7k views Asked by At

I am trying to send a file to server using cURL command from windows command prompt. But getting error message as "{"detail":"JSON parse error - No JSON object could be decoded"}". But using postman I am able to send a file to server. Below given details.

These are the two cURL commands I tried to send/upload file by just sending file location:

curl --insecure -F user_file=@"C:/Users/402096/Desktop/dau_settings-123.conf" -H "Content-Type: application/json" -H "Authorization: Token 3f98d0a178ddd176faea94e6d629621920d203b2624550e68a" -X POST https://10.107.12.123/import_config

curl --insecure -F "user_file=@C:/Users/402096/Desktop/dau_settings-123.conf" -H "Content-Type: application/json" -H "Authorization: Token 3f98d0a178ddd176faea94e6d629621920d203b2624550e68a" -X POST https://10.107.12.123/import_config

Here is the my code which will be called to upload a file in django

@api_view(["POST", "GET"])
@permission_classes((IsAuthenticated,))
@permission_required('dau_gui_app.execute_import_config')   
def import_config_view(request):
    
    if request.method == 'POST' and request.FILES['user_file']:
        
        user_file = request.FILES['user_file']         
       
        fs = FileSystemStorage()
        filename = fs.save( user_file.name, user_file)
        
        filePath = fs.location + "/" + filename
        
        print filePath
        
        message ="{\
                \"command\":\"execute\",\
                \"subcommand\":\"import_config\",\
                \"args\": [{\"file_path\":\"" + filePath + "\"}]}"    
                
        message.replace(" ", "")
        
        try:
            response = send_cmd(message) 
        except:
            print("An exception occurred sending the message:")   
            print(message)   
            
        #end try       
        
        fs.delete(filename)
                       
        return HttpResponse(response, content_type='application/json')
        
    return render(request, 'dau_gui_app/import_config.html' ,{'title':"Import Configuration" })  

NOTE: I am able to successfully upload a file using postman. When I looked at the cURL code that postman has used to upload/send a file, I see below cURL in postman. But when I typed same command on windows command prompt, then getting errors related to syntax and format.

curl -X POST 'https://10.107.12.123/import_config' -H 'Content-Type: application/json' -H 'Authorization: Token 3f98d0a178ddd176faea94e6d629621920d203b2624550e68a' -H 'Cookie: csrftoken=wC4LbjwDsh5BkRPi7TgwZN2FbQLMMpuz; sessionid=7xq7jv95j50k6lkojbrkp2ndgfczfe9v' -F 'user_file=@/C:/Users/402096/Desktop/dau_settings-123.conf'

Please help me how can I send/upload a file to my django server using cURL from windows command prompt. Not sure what I missed here

1

There are 1 answers

1
Sushrut Ashtikar On

You need to install curl and set the path in the environment variables. Checkout installation steps from here: https://develop.zendesk.com/hc/en-us/articles/360001068567-Installing-and-using-cURL#install