Hi, I am new to development with Web API's. I have figured out how to build and send request to GET data from the the API. I thought I had coded my routines to be able to send a post request, but I have not figured out how to send my JSON string with the data to be posted.
I have included my code and any and all help is greatly appreciated.
Public Async Function MakeRequest(verb As String, resource As String) As Task
Dim client As HttpClient = New HttpClient
Dim response As Task(Of HttpResponseMessage)
Dim result As HttpResponseMessage = New HttpResponseMessage
Dim request As HttpRequestMessage = New HttpRequestMessage()
Dim requestp As HttpWebRequest
Dim queryString As String = Nothing
' determine sandbox or live there
RequestGood = False
If My.Application.CommandLineArgs(0).ToString() = "S" Then
uri = New Uri(sandboxuri)
Else
uri = New Uri(liveuri)
End If
commapikey = "21DDA0A8-9F6B-498A-BD1E-0116C308A263"
sSignature = GetAuthHeader(softwarekey, apiclientid)
client = CreateClient(uri, commapikey, apiversion, sSignature)
headers = GetHeaders(commapikey, apiversion, sSignature)
Select Case verb
Case "GET"
request = BuildRequest(client, HttpMethod.Get, String.Format("{1}?subscription-key={0}", subscriptionkey, resource))
Case "PUT"
request = BuildRequest(client, HttpMethod.Put, String.Format("{1}?subscription-key={0}", subscriptionkey, resource))
Case "POST"
request = BuildRequest(client, HttpMethod.Post, String.Format("{1}?subscription-key={0}", subscriptionkey, resource))
Case Else
request = BuildRequest(client, HttpMethod.Get, String.Format("{1}?subscription-key={0}", subscriptionkey, resource))
End Select
response = client.SendAsync(request)
result = response.Result
If result.StatusCode = HttpStatusCode.BadRequest Then
' error
Else
json = result.Content.ReadAsStringAsync().Result
RequestGood = True
End If
End Function
Public Function GetAuthHeader(ByVal key As String, ByVal message As String) As String
Dim sToken As String = String.Format("{0}:{1}", message, key)
Dim sReturn As String = Nothing
Dim bTokenBytes() = System.Text.ASCIIEncoding.UTF8.GetBytes(sToken)
sReturn = String.Format("Basic {0}", Convert.ToBase64String(bTokenBytes))
Return sReturn
End Function
Public Function CreateClient(ByVal config As Uri, ByVal commApiKey As String, ByVal APIVer As String, ByVal APISignature As String) As HttpClient
Dim handler As HttpClientHandler = New HttpClientHandler
Dim client As New HttpClient(handler)
client.BaseAddress = config
client.DefaultRequestHeaders.Accept.Add(New MediaTypeWithQualityHeaderValue("application/json"))
client.DefaultRequestHeaders.Add("community-api-key", commApiKey)
client.DefaultRequestHeaders.Add("api-version", APIVer)
client.DefaultRequestHeaders.Add("authorization", APISignature)
If json <> Nothing Then
client.DefaultRequestHeaders.Add("data", json)
End If
Return client
End Function
Public Function GetHeaders(ByVal commApiKey As String, ByVal APIVer As String, ByVal APISignature As String) As String
Dim sb As New StringBuilder()
sb.Append(String.Format("community-api-key:{0}" & Chr(10), commApiKey))
sb.Append(String.Format("api-version:{0}" & Chr(10), APIVer))
sb.Append(String.Format("authorization:{0}" & Chr(10), APISignature))
Return sb.ToString().ToLower()
End Function
Public Function BuildRequest(ByVal client As HttpClient, ByVal method As HttpMethod, ByVal uri As String) As HttpRequestMessage
Dim request As HttpRequestMessage = New HttpRequestMessage()
request.Headers.Accept.Add(New MediaTypeWithQualityHeaderValue("application/json"))
request.Method = method
request.RequestUri = New Uri(client.BaseAddress, uri)
Return request
End Function