0

I'm trying to get data out of this one [https://developer.mapquest.com/documentation/geocoding-api/reverse/get](MapQuest Reverse Geocode)

$ApiKey = "xxxxx" #Add your key here

$Url = "http://www.mapquestapi.com/geocoding/v1/reverse?key=$ApiKey"
$ApiBody = @{ 
  location = @{
    latLng = @{
      'lat' = 55.40988888
      'lng' = 11.39666277
    }
  }
  options = @{
    'thumbMaps' = 'false'
  }
  includeNearestIntersection = 'false'
  includeRoadMetadata = 'true'
}
write-host ( $ApiBody | ConvertTo-Json)

$response =   Invoke-RestMethod $Url -Method get -Headers @{ 'Content-Type' = 'application/json' } -Body $ApiBody 

#TEST 2
$Url = "http://www.mapquestapi.com/geocoding/v1/reverse?key=$ApiKey"
$ApiBody = @{
  'location' = @{
    'latLng' = @{
      'lat'= 55.40988888
      'lng'= 11.39666277
    }
  }
}
#$ApiBody = $ApiBody | ConvertTo-Json

$response =   Invoke-RestMethod $Url -Method Post  -Body $ApiBody 
$response 

I keep getting a 400 response :/

enter image description here

When I do it in PostMan it works:

PostMan

It seems that I don't format the body form correctly !?

Still it wont work

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • GET means the request does not have a body. Post means the request has a body. You are using GET and should be using Post. Postman is automatically using POST while PowerShell you have to specify the type. – jdweng Mar 04 '23 at 21:57
  • It's seems Powershell `Invoke-RestMethod` GET is blocked body in [here](https://stackoverflow.com/questions/49899476/invoke-restmethod-error-cannot-send-body-type) – Bench Vue Mar 05 '23 at 02:43
  • I've changed PowerShell to POST $Url = "http://www.mapquestapi.com/geocoding/v1/reverse?key=$ApiKey" $ApiBody = @{ 'location' = @{ 'latLng' = @{ 'lat'= 55.40988888 'lng'= 11.39666277 } } } #$ApiBody = $ApiBody | ConvertTo-Json $response = Invoke-RestMethod $Url -Method Post -Body $ApiBody $response Still it wont work – Morten Bach Mar 05 '23 at 09:45
  • I'd played around with the BODY part ... This works $response = Invoke-RestMethod $Url -Method Post -Headers @{ 'Content-Type' = 'application/json' } -Body ( $ApiBody | ConvertTo-Json ) – Morten Bach Mar 05 '23 at 10:09
  • POST call can send body but I tested GET can't send a body. I think you needs find other method like curl, node.js. – Bench Vue Mar 05 '23 at 13:16
  • I tested this command $response = Invoke-RestMethod -Uri 'http://www.mapquestapi.com/geocoding/v1/reverse?key=xxxx' -Method GET -Body ($ApiBody|ConvertTo-Json) -ContentType "application/json" – Bench Vue Mar 05 '23 at 13:17

1 Answers1

0

It was a combo of POST/GET and the body not correctly formatted as JSON

End Result is

Invoke-RestMethod $Url -Method POST -Headers @{ 'Content-Type' = 'application/json' } -Body ( $ApiBody | ConvertTo-Json )
buddemat
  • 4,552
  • 14
  • 29
  • 49