RestAPI via Powershell 3 Invoke-RestMethod

2,438 views
Skip to first unread message

Chris

unread,
Sep 27, 2011, 1:23:56 PM9/27/11
to ravendb
I'm trying to create a document using the new invoke-restmethod
cmdlet.

NAME
Invoke-RestMethod

SYNTAX
Invoke-RestMethod [-Uri] <Uri> [-Method <WebRequestMethod>
{Default | Get | Head | Post | Put | Delete | Trace |
Options}] [-ReturnType <RestReturnType> {Detect | Json | Xml}] [-
Session <WebRequestSession>] [-SessionVariable
<string>] [-Credential <PSCredential>] [-UseDefaultCredentials] [-
CertificateThumbprint <string>] [-Certificate
<X509Certificate>] [-UserAgent <string>] [-DisableKeepAlive] [-
Timeout <int>] [-Headers <IDictionary>]
[-MaximumRedirection <int>] [-Proxy <Uri>] [-ProxyCredential
<PSCredential>] [-ProxyUseDefaultCredentials] [-Body
<Object>] [-ContentType <string>] [-TransferEncoding <string>] [-
InFile <string>] [-OutFile <string>] [-PassThru]
[<CommonParameters>]

PS > Invoke-RestMethod -Uri $url -method PUT -Credential $cred -Body
$jsondata -ContentType "application/json"

I keep getting "Invoke-RestMethod : The remote server returned an
error: (401) Unauthorized" in response.

I've tried passing my credentials. I've tried setting the contenttype
to "application/json"

I can't find any log entry to suggest why the request was denied.

GET requests work fine.

Anyone have any luck with this yet?

Ayende Rahien

unread,
Sep 28, 2011, 12:00:32 AM9/28/11
to rav...@googlegroups.com
What does it looks like in Fiddler?

Chris

unread,
Sep 28, 2011, 11:43:42 AM9/28/11
to ravendb
I did get it to work by using the -UseDefaultCredentials switch. I'm
not sure what it does differently than using -Credential and a
supplied PSCredential object.

# Bad Request
PUT http://dul-524-l7:8080/docs/newdoc HTTP/1.1
User-Agent: Mozilla/5.0 (Windows NT; Windows NT 6.1; en-US)
WindowsPowerShell/3.0
Content-Type: application/json
Authorization: Negotiate oYIGmjCCBpagAwoBAaKC... 2261 random
characters
Host: dul-524-l7:8080
Content-Length: 16

{"name":"chris"}


#Working Request
PUT http://dul-524-l7:8080/docs/newdoc HTTP/1.1
User-Agent: Mozilla/5.0 (Windows NT; Windows NT 6.1; en-US)
WindowsPowerShell/3.0
Content-Type: application/json
Authorization: Negotiate oXcwdaADCgEBoloEWE5UTE1TU1AAA...165 random
characters
Host: dul-524-l7:8080
Content-Length: 16

{"name":"chris"}
Reply all
Reply to author
Forward
0 new messages