0

I'm trying to call a WebAPI2 api from a legacy VB code.

The API works when called from fiddler or from the AngularJS client.

[Route("CreateMyObject")]
[HttpPost]
public async Task<JsonResult<MyObject>> CreateMyObject([FromUri] int parentId, [FromBody] MyObject object)

Then in the VB code:

Dim apiUri As New Uri(apiUrl & "api/CreateMyObject?parentId=" &
intParentId.ToString())
Dim data = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(myObjectInstance))
Dim webRequest As WebRequest = WebRequest.Create(uri)
webRequest.ContentType = "application/json"
webRequest.Method = "POST"

webRequest.ContentLength = data.Length
Dim stream = webRequest.GetRequestStream()
stream.Write(data, 0, data.Length)
stream.Close()

Dim response = webRequest.GetResponse().GetResponseStream()
Dim reader As New StreamReader(response)
Dim res = reader.ReadToEnd()
reader.Close()
response.Close()

I can only get a 415 response from the API. I tried other content-types as well, but with the same result or server error.

Whatever is wrong with this call seems to be related with the Json body bit, because if I don't send anything in the call body, the parentId gets to the API as it's supposed to.

JBourne
  • 180
  • 2
  • 10

1 Answers1

0

As Chase Rocker mentioned, try first by initializing jsonDataBytes as

Dim jsonDataBytes As Bytes()

Also verify the json object to make sure it is fine. Use this link http://jsonlint.com/ or for better assistance use Fiddler instead.

dotnetspark
  • 551
  • 4
  • 23