I’m trying to communicate with a 3rd party API and their specs state the content-type must be application/x-www-form-urlencoded but the body is formatted as json. (yes I tried sending them as a type json and they reject it.) The below sample code works fine setting the content-type to json: (sorry it’s in VB, this is a 20 year old system that I inherited)
Protected Async Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
'JSON
Dim client As New HttpClient
Dim url = $"http://localhost:99/api/json"
Dim payload = "{""test"":""hi"",""test2"":{""Nest1"":""hello"",""Next2"":""World""}}"
Dim c As New StringContent(payload, Encoding.UTF8, "application/json")
Dim myRequest = New HttpRequestMessage
myRequest.RequestUri = New Uri(url)
myRequest.Content = c
myRequest.Method = HttpMethod.Post
Using response As HttpResponseMessage = Await client.SendAsync(myRequest)
Dim token = Await response.Content.ReadAsStringAsync
Label1.Text = token
End Using
End Sub
with the expected results of
{ "test": "hi", "test2": { "Nest1": "hello", "Next2": "World" } }
When I set the mediaType property to be urlencoded, not surprisingly, the body is encoded and wrapped in {“(myStuff)”}
{ "{"test":"hi","test2":{"Nest1":"hello","Next2":"World"}}": "" }
Is there a way to keep the content-type to be URLencoded but force the “raw” json string to be sent without the enclosing brackets?