1.定义JSON字符串变量
PS C:\WINDOWS\system32> $body = @'{"Method": "POST","Body": {"model": "deepseek-r1","messages": [{"content": "why is the sky blue?","role": "user"}]},"Uri": "http://localhost:11434/api/generate"
}
'@PS C:\WINDOWS\system32> $body{"Method": "POST","Body": {"model": "deepseek-r1","messages": [{"content": "why is the sky blue?","role": "user"}]},"Uri": "http://localhost:11434/api/generate"
}
- 将 JSON 字符串转换为 PowerShell 对象
PS C:\WINDOWS\system32> $bodyObj = $body | ConvertFrom-JsonPS C:\WINDOWS\system32> $bodyObj.Bodymodel messages
----- --------
deepseek-r1 {@{content=why is the sky blue?; role=user}}PS C:\WINDOWS\system32> $bodyObj.Body.messagescontent role
------- ----
3.从 PowerShell 对象生成 JSON 字符串
PS C:\WINDOWS\system32> $bodyObj = @{Uri = 'http://localhost:11434/api/generate' Method = 'POST'Body = @{model = "deepseek-r1"messages = @(@{ role = "user"content="why is the sky blue?" })}
}PS C:\WINDOWS\system32> $bodyObj | ConvertTo-Json -Depth 5
{"Method": "POST","Body": {"model": "deepseek-r1","messages": [{"content": "why is the sky blue?","role": "user"}]},"Uri": "http://localhost:11434/api/generate"
}
- Invoke-WebRequest的body参数支持中文
PS C:\Users\houyingshi> $body = @' {"model": "deepseek-r1","prompt": "天空为什么是蓝色的","stream": false
}
'@PS C:\Users\houyingshi> (Invoke-WebRequest -method POST -Body $body -ContentType "application/json; charset=utf-8" -uri http://localhost:11434/api/generate ).Content | ConvertFrom-json