Powershell中的POST方法给出400错误请求

时间:2020-01-17 08:52:09

标签: python powershell rest post flask

我正在尝试将系统信息直接从此PS脚本发布到我的Python API,当我将Postman中Powershell中的JSON数据发布到API时,它工作得很好,但是当我尝试在PowerShell中进行操作时,我不断以下错误:

400 Bad Request
Bad Request
The browser (or proxy) sent a request that this server could not understand.
At line:1 char:71
+ ... rtTo-Json | Invoke-RestMethod -Uri "127.0.0.1:5000/event" -Method Pos ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

这是PS代码:

$Result = Get-EventLog -LogName Application -Entrytype Error
$url = "http://127.0.0.1:5000/event"
Invoke-RestMethod -Uri $url -Method POST -Body ($Result|ConvertTo-Json) -ContentType "application/json"

Python代码:(从PS发布但代码为400时,它会在True上打印request.is_json

@app.route('/event', methods=['POST'])
def postEventViewer():
    print(request.is_json)
    content = request.get_json()
    for item in content:
        machinename = item['MachineName']
        eventid = item['EventID']
        message = item['Message']
        source = item['Source']

        print(machinename)
        return 'Successful'

我在这里做错了什么?谢谢!

0 个答案:

没有答案