Click here to Skip to main content
15,943,008 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
my VB skills are "BASIC" but I just need to create a simple app that checks if a user exists on website we use which works successfully, if they don't exist allows user to add the user.

the website has given code to read but none to write, so have tried to do it myself, but when I post it replies back.

StatusCode 415 "Unsupported media type"

I know this code is dirty but forgive me don't have much time so just need to get job done.

Dim theclient As New HttpClient()
theclient.BaseAddress = New Uri("") 'Base Url

' Add an Accept header for JSON format.

Dim toEncodeAsBytes As Byte()
Dim thetext As String = apiAccessKey & ":" & apiSecretKey
toEncodeAsBytes =

Dim returnValue As String = System.Convert.ToBase64String(toEncodeAsBytes)

' Add Authorization header
theclient.DefaultRequestHeaders.Authorization = New AuthenticationHeaderValue("Basic", returnValue)

' Response.

Dim userdetails As String = "{" & Chr(13) & Chr(34) & "Username" & Chr(34) & ": " & Chr(34) & firstname & "." & lastname & Chr(34) & "," & Chr(13)
userdetails = userdetails & Chr(34) & "Firstname" & Chr(34) & ": " & Chr(34) & firstname & Chr(34) & "," & Chr(13)
userdetails = userdetails & Chr(34) & "Lastname" & Chr(34) & ": " & Chr(34) & lastname & Chr(34) & "," & Chr(13)
userdetails = userdetails & Chr(34) & "Password" & Chr(34) & ": " & Chr(34) & "Password1" & Chr(34) & "," & Chr(13)
userdetails = userdetails & Chr(34) & "Email" & Chr(34) & ": " & Chr(34) & emailaddress & Chr(34) & "," & Chr(13)
userdetails = userdetails & "}"

Dim encode As Byte()
encode = System.Text.ASCIIEncoding.ASCII.GetBytes(Convert.ToString(userdetails))

Dim userdata As String = System.Convert.ToBase64String(encode)

Dim response As HttpResponseMessage = theclient.PostAsync("api/users/createUser", New StringContent(userdata)).Result '
Dim responsetext As String = response.ToString

any help on where I am going wrong would be appreciated

What I have tried:

I tried sending it as a string and encoding it and both came back with same basic error.
Updated 19-Mar-21 4:54am

The reason that you're getting that error message back is because the API endpoint you're hitting is expecting the request payload to be in a particular format. What you're posting is a base64 encoded string, which the endpoint doesn't support.

You need to check the documentation for the endpoint and make sure what you're sending is compatible. If the endpoint is expecting JSON content then you need to stop encoding into base64, and ensure that the StringContent is regarded as JSON by adding the mime-type parameter new StringContent(userdetails, System.Text.Encoding.UTF8, "application/json")
Share this answer
Thank you Chris that worked a treat
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900