【问题标题】:Is there a way to upload file in PowerShell to rest api有没有办法在 PowerShell 中上传文件来休息 api
【发布时间】:2020-07-27 16:30:31
【问题描述】:

我正在寻找一种使用 Powershell 将文件上传到用 Python 编写的 api 的方法。

服务器端: 导入操作系统 从烧瓶导入烧瓶,jsonify,请求,中止,闪存,重定向,url_for 从 werkzeug.utils 导入安全文件名

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)

来自 Powershell 的请求:

Invoke-WebRequest -uri 'http://127.0.0.1:5000/attachmnt' -Method Post -Infile "./test.jpg"  -ContentType 'image/jpg'  -UseDefaultCredentials 

【问题讨论】:

  • 你是说你在这里失败了?您没有在帖子中显示任何错误。 API 所写的内容应该没有太大区别,只是您正确使用了 API,按照它的预期传递它。
  • 这个答案在类似的情况下帮助了我。 stackoverflow.com/a/50255917/11406870

标签: python powershell


【解决方案1】:

Invoke-RestMethod 是用于 Rest 的更谨慎的 cmldet,因为这是它的目的。

Invoke-RestMethod | Microsoft Docs

  • 模块:Microsoft.PowerShell.Utility
  • 向 RESTful Web 服务发送 HTTP 或 HTTPS 请求。

Simple Examples of PowerShell's Invoke-RestMethod

简单的 GET 示例

$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 示例

$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 示例

$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'

删除示例

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-19
    • 1970-01-01
    • 2021-08-21
    • 1970-01-01
    • 2015-11-11
    • 2016-04-12
    相关资源
    最近更新 更多