Search code examples
apachevbaexceltomcatwinhttp

Wrong String parameter when sending a json string as a post request


I used this code for my Macro in VBA in excel:

Sub Macro1()

Dim URL As String, JSONString As String, objHTTP As Object
Set objHTTP = CreateObject("WinHttp.WinHttpRequest.5.1")
URL = "xxxxx"
objHTTP.Open "POST", URL, False
objHTTP.setRequestHeader "Content-type", "application/x-www-form-urlencoded"
objHTTP.setRequestHeader "User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"
JSONString = "{""name"": ""long"",""startDate"": 12345,""endDate"": 67890,""status"": ""in progress""}"
objHTTP.send JSONString
Debug.Print objHTTP.Status
Debug.Print objHTTP.responseText

End Sub

In the immediate window, I received this error:

400 Apache Tomcat/8.0.28 - Error reportH1 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:22px;} H2 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:16px;} H3 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:14px;} BODY {font-family:Tahoma,Arial,sans-serif;color:black;background-color:white;} B {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;} P {font-family:Tahoma,Arial,sans-serif;background:white;color:black;font-size:12px;}A {color : black;}A.name {color : black;}.line {height: 1px; background-color: #525D76; border: none;}

HTTP Status 400 - Required String parameter 'name' is not present

type Status report

message Required String parameter 'name' is not present

description The request sent by the client was syntactica lly incorrect.

Apache Tomcat/8.0.28

I have not known how to solve it, please help me !


Solution

  • I solved this problem:
    
    Sub Macro1()
    Dim URL As String, JSONString As String, objHTTP As Object
    Set objHTTP = CreateObject("WinHttp.WinHttpRequest.5.1")
    URL = "xxxxx?name=long&startDate=12345&endDate=67890&status=in progress"
    objHTTP.Open "POST", URL, False
    objHTTP.setRequestHeader "Content-type", "application/x-www-form-urlencoded"
    objHTTP.setRequestHeader "User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"
    
    objHTTP.send
    Debug.Print objHTTP.Status
    Debug.Print objHTTP.responseText
    End Sub