프로젝트 및 실습/VBA

[VBA] Http Request 보내기

jooona 2022. 8. 11. 18:26
반응형

다음과 같이 Http Request를 보낼 수 있습니다.

Sub requestHTTP()

    Dim Body As String
    Dim httpRequest As New WinHttpRequest

    Body = "{""TYPE"": "".type1"",""VER"": ""1.0"",""USER_ID"": ""123456789""}"
    With httpRequest
        .Open "POST", "https://eo5o12lm6p2y86s.m.pipedream.net"
        .SetRequestHeader "Content-Type", "application/json"
        .Send Body
        .WaitForResponse: DoEvents
        strText = .ResponseText
    End With
    
    MsgBox httpRequest.Status
    MsgBox strText
    
End Sub

 

1. Body에 보내고자 하는 데이터 삽입

2. Open 뒤에 원하는 Http Method (Get, Post 등)와 목적지 url

3. SetRequestHeader에 원하는 콘텐츠 타입 지정

4. ResponseText는 받아오는 메시지

5. Status를 통해 요청의 Status Code 확인 가능

 

Pipedream이라는 사이트에서 Request가 잘 가는지 확인해 볼 수 있습니다. 

 

www.requestbin.com  

 

RequestBin.com — A modern request bin to collect, inspect and debug HTTP requests and webhooks

 

requestbin.com

 

 

위 사진과 같이 보낸 대로 잘 받아오는 것을 확인할 수 있습니다. 아래는 MsgBox로 출력한 Status Code입니다.

 

 

 

반응형

'프로젝트 및 실습 > VBA' 카테고리의 다른 글

[VBA] File Dialog를 사용해 파일 선택  (0) 2022.08.22
[VBA] SGN Function  (0) 2022.08.22
[VBA] 행, 열 숨기기  (0) 2022.08.08
[VBA] Join Function  (0) 2022.08.08
[VBA] ParamArray  (0) 2022.08.05