Link to home
Create AccountLog in
Avatar of Wayne Hayes
Wayne HayesFlag for Australia

asked on

Convert Postman C# HttpClient POST Code to VBA using WinHttp.WinHttpRequest.5.1

Requesting assistance to convert 'Postman' POST API call from C#-httpClient to VBA (Visual Basic) using WinHttp.WinHttpRequest.5.1.  Postman provides code snippets for many programming languages but not VB/VBA.  I have been able to successfully determine VBA code for other events - "Content-Type", "application/json" but unable to convert code using "Content-Type" "multipart/form-data".  

API Documentation provided from vendor as follows:- 
' API Message Send

' Method    Operation

' POST      /snd/message/send/<client>


'Request Parameters

'Type                   Params                  Values                      Description

'HEAD                   Content-Type            multipart/form-data

'HEAD                   Authorization           Bearer <id_token>           Token from Authenticate call

'PATH                   client                  <client>                    Client ID – 10 digit number

'FORM                   uploadToken             <uploadToken>               Token received from Message Upload Token call

'FORM                   sender                  <sender>                    participant identifier of sender

'FORM                   recipient               <recipient>                 participant identifier of recipient

'FORM                   payloadFile             <payloadFile>               Document XML to upload


Postman code snippet:-

var client = new HttpClient();

var request = new HttpRequestMessage(HttpMethod.Post, "https://api-ebusiness/snd/message/send/77777777");

request.Headers.Add("Authorization", "Bearer eyJhbGciOiJIUzUxMiJ9");

var content = new MultipartFormDataContent();

content.Add(new StringContent("0151:99999999999"), "sender");

content.Add(new StringContent("0151:88888888888"), "recipient");

content.Add(new StreamContent(File.OpenRead("/C:/Documents/User/Run200000085.xml")), "payloadFile", "/C:/Documents/User/Run200000085.xml");

content.Add(new StringContent("5785f22f-fbae-4110-a4ee-b093ac67d756@B2BToken"), "uploadToken");

request.Content = content;

var response = await client.SendAsync(request);

response.EnsureSuccessStatusCode();

Console.WriteLine(await response.Content.ReadAsStringAsync());


Thanks.

Avatar of David Johnson, CD
David Johnson, CD
Flag of Canada image


Set objHTTP = CreateObject("MSXML2.ServerXMLHTTP")

URL = "http://www.somedomain.com"

objHTTP.Open "POST", URL, False

objHTTP.setRequestHeader "User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"

objHTTP.send ""




Sub macroPOST()

    Dim objHTTP As Object

    Dim Url As String

    Dim data As Dictionary


    Set objHTTP = CreateObject("MSXML2.serverXMLHTTP")

    Set json_dict = New Dictionary

    

    Set data_dict = New Dictionary 'Creating a new dictionary from a table of excel where the Headers are G5, H5 and I5 .

    data_dict(Range("G5").Value) = Range("G6:G8").Value

    data_dict(Range("H5").Value) = Range("H6:H8").Value

    data_dict(Range("I5").Value) = Range("I6:I8").Value



    json_dict("stream_key") = Range("B1").Value 'Taking value from excel

    json_dict("return_data") = "False"

    json_dict("env") = Range("B4").Value 'Taking value from excel

    

    json_dict.Add "data", data_dict ' Adding dictionary of data to another dictionary. 

    

    TempTxt = JsonConverter.ConvertToJson(json_dict) ' Finally generating a json from dict

    

    Debug.Print TempTxt

    Url = Range("B2").Value

    

    objHTTP.Open "POST", Url, False

    objHTTP.setRequestHeader "Content-Type", "application/json" ' define json to send data as json

    objHTTP.send TempTxt      

    If objHTTP.Status = "200" Then 'success

    Else

        'Do something

    End If

    Debug.Print objHTTP.responseText

End Sub

change content-type from application/json to application/xml

ASKER CERTIFIED SOLUTION
Avatar of Wayne Hayes
Wayne Hayes
Flag of Australia image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account