Search code examples
powershellinvoke-webrequestinvoke-restmethod

Querying an API which is returning failed, formatting error somewhere


I'm trying to query this API https://docs.iocparser.com/api-reference/parse-api

I'm getting a failure to resolve URL error, which makes me believe that somewhere in this line I've gone wrong with the formatting, but I can't figure out where, so any help would be appreciated.

$Request = Invoke-RestMethod -Method Post 
                 -Uri 'https://api.iocparser.com/url' 
                 -Headers @{"Content-Type" = "application/json"} 
                 -Body @{'url' = 'https://pastebin.com/raw/rgnvuYi2'} 
                 -Verbose

This is the error I'm getting back.

Invoke-RestMethod : {"status": "error", "error": "IOC Parser failed to resolve 
the given URL"}

Solution

  • Explicitly convert the Body data to a JSON string. I do remember Invoke-RestMethod doing it automatically before, but in this case, it is not.

    Invoke-RestMethod -Method Post -Uri 'https://api.iocparser.com/url' -Headers @{"Content-Type" = "application/json"} -Body (@{'url' = 'https://pastebin.com/raw/rgnvuYi2'} | ConvertTo-Json) -Verbose