3

I have a bog standard WebAPI that accepts a POST and takes those parameters and processes certain things. I'm not certain if the problem is the VBA or the ASP.Net WebAPI so I am cross posting.

I've used Postman to test the API and it works fine when I post Key/Value params.

I've also tried the following method and relevant parameters and get the same result:

Set objHTTP = CreateObject("MSXML2.ServerXMLHTTP")

I'm having problems getting it to work with the VBA, when debugging in Visual Studio on the WebAPI I cannot see the values I am posting anywhere. It's like the request is coming in blank. My WebAPI is subsequently throwing an error because the parameters are missing and certain parameters are required.

I'm not sure if this is a problem on the VBA side or the ASP.NET WebAPI side so I am cross posting in a hope someone can highlight or spot what I am doing wrong.

Private Sub Command4_Click()

Dim argumentString1 As String

argumentString1 = "companyId=228&secondsToLog=15&subject=TestBackup123&description=TestDescription" & _
"&category=&tag=&ticketType=task&assignee=gavin&[email protected]" & _
"&submitterName=gavin&status=open&priority=normal"

Set objHTTP = CreateObject("WinHttp.WinHttpRequest.5.1")
URL = "http://localhost:64874/api/zendeskhelper"
objHTTP.Open "POST", URL, False
objHTTP.setRequestHeader "Content-type", "application/x-www-form-urlencoded"
objHTTP.send (argumentString1)


txtresult = objHTTP.responsetext & ": " & argumentString1
End Sub

The Web API structure looks like this:

public HttpResponseMessage Post([FromUri] TicketBody ticket)
{
// Nothing is bound to ticket like it is in Postman
}

Any help or pointers would be much appreciated!

3
  • Have you tried without [FromUri]? Commented Jun 20, 2016 at 8:41
  • I have now and the parameters are now getting passed! I'll have to read up on what the FromURI actually does. If you want to post this as the solution I'll mark it as such! Commented Jun 20, 2016 at 8:52
  • There's also [FromBody] to specify that a parameter should only come from the body of the request. Commented Jun 20, 2016 at 15:25

1 Answer 1

3

The way you are sending your data, you need to remove [FromUri]. When using [FromUri], the ASP.NET engine will look for data in the Uri, not the body of the request.

public HttpResponseMessage Post(TicketBody ticket)
{
    // 'ticket should not be null now
}

On the other hand, if you need to keep [FromUri] you could change your call to:

Set objHTTP = CreateObject("WinHttp.WinHttpRequest.5.1")
URL = "http://localhost:64874/api/zendeskhelper?" & argumentString1 
objHTTP.Open "POST", URL, False
objHTTP.setRequestHeader "Content-type", "application/x-www-form-urlencoded"
objHTTP.send ("")
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.