programing

파라미터가 있는 Invoke-WebRequest, POST

telecom 2023. 4. 15. 08:22
반응형

파라미터가 있는 Invoke-WebRequest, POST

uri에 POST를 시도하고 파라미터를 송신하려고 합니다.username=me

Invoke-WebRequest -Uri http://example.com/foobar -Method POST

POST 방법을 사용하여 파라미터를 전달하려면 어떻게 해야 합니까?

파라미터를 해시테이블에 넣고 다음과 같이 전달합니다.

$postParams = @{username='me';moredata='qwerty'}
Invoke-WebRequest -Uri http://example.com/foobar -Method POST -Body $postParams

일부 까다로운 웹 서비스의 경우 요청의 콘텐츠 유형을 JSON으로 설정하고 본문을 JSON 문자열로 설정해야 합니다.예를 들어 다음과 같습니다.

Invoke-WebRequest -UseBasicParsing http://example.com/service -ContentType "application/json" -Method POST -Body "{ 'ItemID':3661515, 'Name':'test'}"

또는 XML에 상당하는 것 등입니다.

이 방법은 유효합니다.

$body = @{
 "UserSessionId"="12345678"
 "OptionalEmail"="MyEmail@gmail.com"
} | ConvertTo-Json

$header = @{
 "Accept"="application/json"
 "connectapitoken"="97fe6ab5b1a640909551e36a071ce9ed"
 "Content-Type"="application/json"
} 

Invoke-RestMethod -Uri "http://MyServer/WSVistaWebClient/RESTService.svc/member/search" -Method 'Post' -Body $body -Headers $header | ConvertTo-HTML

사용할 때 ps 변수를 사용하지 않는 단일 명령어JSON한 몸으로{lastName:"doe"}POST API 호출의 경우:

Invoke-WebRequest -Headers @{"Authorization" = "Bearer N-1234ulmMGhsDsCAEAzmo1tChSsq323sIkk4Zq9"} `
                  -Method POST `
                  -Body (@{"lastName"="doe";}|ConvertTo-Json) `
                  -Uri https://api.dummy.com/getUsers `
                  -ContentType application/json

상세보기: PowerShell 전원 켜기

언급URL : https://stackoverflow.com/questions/17325293/invoke-webrequest-post-with-parameters

반응형