0

I am looking for a way to upload file with Powershell to api written in Python.

The server side: import os from flask import Flask, jsonify, request, abort, flash, redirect, url_for from werkzeug.utils import secure_filename

UPLOAD_FOLDER = 'd:/Temp/11aa/'
ALLOWED_EXTENSIONS = {'jpg'}
app = Flask(__name__)



def allowed_file(filename):
    return '.' in filename and \
           filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS

@app.route('/attachmnt', methods=['GET', 'POST'])
def upload_file():

    if request.method == 'POST':
        if 'file' not in request.files:
            flash('No file part')
            return redirect(request.url)
        file = request.files['file']
        # if user does not select file, browser also
        # submit an empty part without filename
        if file.filename == '':
            flash('No selected file')
            return redirect(request.url)
        if file and allowed_file(file.filename):
            filename = secure_filename(file.filename)
            file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
            return redirect(url_for('uploaded_file',
                                    filename=filename))
    return '''
    <!doctype html>
    <title>Upload new File</title>
    <h1>Upload new File</h1>
    <form method=post enctype=multipart/form-data>
      <input type=file name=file>
      <input type=submit value=Upload>
    </form>
    '''




if __name__ == '__main__':
    app.secret_key = 'pwd'
    app.run(debug=True)

The request from Powershell:

Invoke-WebRequest -uri 'http://127.0.0.1:5000/attachmnt' -Method Post -Infile "./test.jpg"  -ContentType 'image/jpg'  -UseDefaultCredentials 
Krisz
  • 701
  • 7
  • 23
  • Are you saying that what you have here failed? You are not showing any errors in your post. What the API is written in should not make much difference, just that you use the API correctly, as pass it what it expects. – postanote Apr 14 '20 at 23:12
  • This answer helped me in a similar situation. https://stackoverflow.com/a/50255917/11406870 – Ben Zikri Dec 22 '21 at 11:39

1 Answers1

0

Invoke-RestMethod is a more prudent cmldet to use for Rest, since that is its purpose.

Invoke-RestMethod | Microsoft Docs

  • Module: Microsoft.PowerShell.Utility
  • Sends an HTTP or HTTPS request to a RESTful web service.

Simple Examples of PowerShell's Invoke-RestMethod

Simple GET example

$response = Invoke-RestMethod 'http://example.com/api/people'
# assuming the response was in this format { "items": [] }
# we can now extract the child people like this
$people = $response.items

GET with custom headers example

$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("X-DATE", '9/29/2014')
$headers.Add("X-SIGNATURE", '234j123l4kl23j41l23k4j')
$headers.Add("X-API-KEY", 'testuser')

$response = Invoke-RestMethod 'http://example.com/api/people/1' -Headers $headers

PUT/POST example

$person = @{
    first='joe'
    lastname='doe'
}
$json = $person | ConvertTo-Json
$response = Invoke-RestMethod 'http://example.com/api/people/1' -Method Put -Body $json -ContentType 'application/json'

DELETE example

$response = Invoke-RestMethod 'http://example.com/api/people/1' -Method Delete
postanote
  • 15,138
  • 2
  • 14
  • 25