# Invoke-RestMethod Issues

**URL:** <https://forums.powershell.org/t/invoke-restmethod-issues/6129>\
**Category:** PowerShell Help\
**Created:** [April 4, 2016, 10:26pm UTC](https://forums.powershell.org/t/invoke-restmethod-issues/6129 "2016-04-04T22:26:31Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ashiqui-donumber](https://avatars.discourse-cdn.com/v4/letter/a/43a26b/32.png) [@ashiqui-donumber](https://forums.powershell.org/u/ashiqui-donumber)\
**Post date:** [April 4, 2016, 10:26pm UTC](https://forums.powershell.org/t/invoke-restmethod-issues/6129/1 "2016-04-04T22:26:31Z")

</div>

I am trying to connect to an external api website. I don’t know details around how REST/JSON works, but I want to use powershell to download a csv file via GET method. I could successfully connect via CURL, but with powershell I cannot, and am exhausted.

CURL:

curl.exe -v -H “Accept: application/json” -u APIKEY: “https://”

Powershell:

Invoke-RestMethod -Uri ‘https://’ -Headers @{“AUTHORIZATION”="Basic "} -Method Get

I always receive following error:

the underlying connection was closed an unexpected error occurred on a send and the underlying connection was closed an unexpected error occurred on a receive

I tried using following script for certificate:

add-type @"  
using [System.Net](http://System.Net);  
using System.Security.Cryptography.X509Certificates;  
public class TrustAllCertsPolicy : ICertificatePolicy {  
public bool CheckValidationResult(  
ServicePoint srvPoint, X509Certificate certificate,  
WebRequest request, int certificateProblem) {  
return true;  
}  
}  
"@  
[System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy

$result = Invoke-WebRequest -Uri “[https://IpAddress/resource](https://IpAddress/resource)”

Source: [.net - Powershell v3 Invoke-WebRequest HTTPS error - Stack Overflow](http://stackoverflow.com/questions/11696944/powershell-v3-invoke-webrequest-https-error)

still no luck.

Can someone help me understand what I am doing wrong?

---

<div class="post-metadata">

**Author:** ![ashiqui-donumber](https://avatars.discourse-cdn.com/v4/letter/a/43a26b/32.png) [@ashiqui-donumber](https://forums.powershell.org/u/ashiqui-donumber)\
**Post date:** [April 4, 2016, 10:34pm UTC](https://forums.powershell.org/t/invoke-restmethod-issues/6129/2 "2016-04-04T22:34:16Z")

</div>

Basic is followed by Base64Encoded API KEY. This is what I see when I use the API Web, the one website provides:

{  
“Accept”: “application/json”,  
“Authorization”: “Basic Base64Encode”,  
“Content-Length”: 0,  
“x-forwarded-for”: “3 IP Addresses Separated by Comma”  
}

---

<div class="post-metadata">

**Author:** ![ashiqui-donumber](https://avatars.discourse-cdn.com/v4/letter/a/43a26b/32.png) [@ashiqui-donumber](https://forums.powershell.org/u/ashiqui-donumber)\
**Post date:** [April 4, 2016, 10:37pm UTC](https://forums.powershell.org/t/invoke-restmethod-issues/6129/3 "2016-04-04T22:37:12Z")

</div>

I am using following powershell version:

PS C:&gt; $PSVersionTable.PSVersion

Major Minor Build Revision

* * *

3 0 -1 -1

---

<div class="post-metadata">

**Author:** ![ashiqui-donumber](https://avatars.discourse-cdn.com/v4/letter/a/43a26b/32.png) [@ashiqui-donumber](https://forums.powershell.org/u/ashiqui-donumber)\
**Post date:** [April 4, 2016, 10:56pm UTC](https://forums.powershell.org/t/invoke-restmethod-issues/6129/4 "2016-04-04T22:56:32Z")

</div>

I upgraded to v4

PS C:&gt; $PSVersionTable.PSVersion

Major Minor Build Revision

* * *

4 0 -1 -1

and also used:

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12

since TLS1.2 is the requirement, still same error:

Invoke-RestMethod : The underlying connection was closed: An unexpected error occurred on a receive.  
At line:1 char:1

- Invoke-RestMethod -Method Get -Uri 'https:////// …
- 

```auto
  + CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], We
 eption
  + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

```

If I don’t use https, then it says:

Unable to connect to the remote server

---

<div class="post-metadata">

**Author:** ![ashiqui-donumber](https://avatars.discourse-cdn.com/v4/letter/a/43a26b/32.png) [@ashiqui-donumber](https://forums.powershell.org/u/ashiqui-donumber)\
**Post date:** [April 9, 2016, 12:14pm UTC](https://forums.powershell.org/t/invoke-restmethod-issues/6129/5 "2016-04-09T12:14:35Z")

</div>

I moved to 2012 R2 server, and the issues resolved.

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:43pm UTC](https://forums.powershell.org/t/invoke-restmethod-issues/6129/6 "2024-05-16T20:43:14Z")

</div>


