Invoke-RestMethod (form-data) is returning that a required field is missing

Will try to keep this short, without leaving out essentials. I’m working on a couple of functions to call an api (links to doc incl below for reference). One is working fine, but the other one keeps failing me due to missing field.

Looking for someone who might have come across something similar and been able to solve it, or simply just has the know-how to see what’s off :slight_smile:

The working one: Calls the api to get verdict on a filehash - https://docs.paloaltonetworks.com/wildfire/7-1/wildfire-api/get-started-with-the-wildfire-api/make-your-first-wildfire-api-call.html

Below is how I have structured that request.

$form = @{
        hash   = $FileHash
        apikey = $ApiKey
        format = $Format
    }
Invoke-RestMethod -Uri $uri -Method Post -Form $form

Working raw request

POST /publicapi/get/report HTTP/1.1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Microsoft Windows 10.0.17763; en-US) PowerShell/6.2.0
Content-Type: multipart/form-data; boundary="a8541dd9-cf50-4138-b8c0-801f99fe2195"
Content-Length: 466
Host: wildfire.paloaltonetworks.com
Connection: close

--a8541dd9-cf50-4138-b8c0-801f99fe2195
Content-Type: text/plain; charset=utf-8
Content-Disposition: form-data; name="hash"

somefilehash
--a8541dd9-cf50-4138-b8c0-801f99fe2195
Content-Type: text/plain; charset=utf-8
Content-Disposition: form-data; name="format"

xml
--a8541dd9-cf50-4138-b8c0-801f99fe2195
Content-Type: text/plain; charset=utf-8
Content-Disposition: form-data; name="apikey"

theapikeystring
--a8541dd9-cf50-4138-b8c0-801f99fe2195--

The troublesome one: Calls the api to submit an url for analysis (+others, but as an example) - https://docs.paloaltonetworks.com/wildfire/9-0/wildfire-api/submit-files-and-links-through-the-wildfire-api/submit-a-website-link-to-wildfire-api.html

I’ve, or at least I think I have, constructed the request the same way in both instances, but the the below throws

Invoke-RestMethod : 'Missing required field apikey'
The function uses a switch depending on what one want to submit, which one might think causes issues. But I have tried adding the fields directly in the hashtable without luck. Due to lack of alternatives I also tried to add the format field (as one do when stuck I guess).
    $form = @{
        apikey = $ApiKey
    }
switch ($PSCmdlet.ParameterSetName) {
    Fileupload {
        $uri = "https://wildfire.paloaltonetworks.com/publicapi/submit/file";
        $form["file"] = $FilePath
    }
    UrlUpload {
        $uri = "https://wildfire.paloaltonetworks.com/publicapi/submit/url";
        $form["url"] = $FileUrl
    }
    SingleWebSiteSubmit {
        $uri = "https://wildfire.paloaltonetworks.com/publicapi/submit/link";
        $form["link"] = $WebSiteUrl
    }
    MultipleWebSiteSubmit {
        $uri = "https://wildfire.paloaltonetworks.com/publicapi/submit/links";
        $form["links"] = $WebSiteUrlFile
    }
    Default {
        $uri = "https://wildfire.paloaltonetworks.com/publicapi/submit/file";
        $form["file"] = $FilePath
    }
}

Invoke-RestMethod -Uri $uri -Method Post -Form $form</pre>

Non-working raw request

POST /publicapi/submit/link HTTP/1.1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Microsoft Windows 10.0.17763; en-US) PowerShell/6.2.0
Content-Type: multipart/form-data; boundary="31f80ee4-eb1a-4c81-961f-bd2a43ce1041"
Content-Length: 337
Host: wildfire.paloaltonetworks.com
Connection: close

--31f80ee4-eb1a-4c81-961f-bd2a43ce1041
Content-Type: text/plain; charset=utf-8
Content-Disposition: form-data; name="link"

http://example.com
--31f80ee4-eb1a-4c81-961f-bd2a43ce1041
Content-Type: text/plain; charset=utf-8
Content-Disposition: form-data; name="apikey"

theapikeystring
--31f80ee4-eb1a-4c81-961f-bd2a43ce1041--

Since its working for the other API , I think it could be an issue in the API. You can try the same with curl.exe with the same examples provided in the documentation. You can install curl for windows and try.

[quote quote=159281]Since its working for the other API , I think it could be an issue in the API. You can try the same with curl.exe with the same examples provided in the documentation. You can install curl for windows and try.

[/quote]
Had the same thought, so already checked that. Both are working fine w/ curl.