首页 > 解决方案 > 对 Mapbox 的 geojson API 请求的 powershell 解析

问题描述

我一直在使用内联 Windows 10 Powershell 命令成功下载静态 Google 和 Mapbox png 图像,但无法让 Powershell 成功传递 geojson 对象以利用 Mapbox 中的覆盖功能。我不是 Powershell 专家用户,我希望我需要添加更多语法才能使其正常工作?

以下代码取自 Mapbox 在线 curl 示例,可以正常工作:

powershell -command "(New-Object System.Net.WebClient).Proxy.Credentials=[System.Net.CredentialCache]::DefaultNetworkCredentials";" 
    Invoke-RestMethod -Uri 'https://api.mapbox.com/styles/v1/mapbox/streets-v10/static/-73.99,40.70,12/500x300?access_token=<access-token-removed-here>' 
    -outfile test_no_overlay.png"

但以下 Mapbox curl 示例使用 geojson 哈希表格式包含覆盖标记(将通过浏览器工作)不能通过 Powershell 工作:

powershell -command "(New-Object System.Net.WebClient).Proxy.Credentials=[System.Net.CredentialCache]::DefaultNetworkCredentials";" 
    Invoke-RestMethod -Uri 'https://api.mapbox.com/styles/v1/mapbox/streets-v10/static/geojson(%7B%22type%22%3A%22Point%22%2C%22coordinates%22%3A%5B-73.99%2C40.7%5D%7D)/-73.99,40.70,12/500x300?access_token=<access-token-removed-here>' 
    -outfile test_overlay.png"

报告的错误是:

Invoke-RestMethod : {"message":"Failed parsing geojson"} At line:1 char:110 + ... redentials; Invoke-RestMethod -Uri ' https://api.mapbox.com/styles/v1/ ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod] , WebException + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

此非代理版本也适用于 Powershell 提示符:

$response=Invoke-RestMethod -Uri 'https://api.mapbox.com/styles/v1/mapbox/streets-v10/static/geojson(%7B%22type%22%3A%22Point%22%2C%22coordinates%22%3A%5B-73.99%2C40.7%5D%7D)/-73.99,40.70,12/500x300?access_token=<access-token-removed-here>'
    -outfile test_overlay.png

所以看来我需要克服一个语法问题才能使其以单行 CMD 格式执行?

标签: powershellcurlmapboxgeojson

解决方案


推荐阅读