Search code examples
c#httpclient

System.FormatException in a mediaType for a httpclient get request


I need to make a request using this specific mediatype application/vnd.wolt.order+json;version=2beta1

Here is my code:

 var client = new HttpClient();
    var request = new HttpRequestMessage(HttpMethod.Get, "https://pos-integration-service.wolt.com/orders/66e41e1459cade439d415f8f");
    request.Headers.Add("WOLT-API-KEY", "xxxx");
    var content = new StringContent(string.Empty);
    content.Headers.ContentType = new MediaTypeHeaderValue("application/vnd.wolt.order+json;version=2beta1");
    request.Content = content;
    var response = await client.SendAsync(request);
    response.EnsureSuccessStatusCode();
    Console.WriteLine(await response.Content.ReadAsStringAsync());

The error i get is that application/vnd.wolt.order+json;version=2beta1 is not valid. I have copy c# code from postman. In postman request works fine I dont know what is wrong


Solution

  • That constructor expects just a media type, not parameters, so ;version=2beta1 gets rejected.

    You need to use MediaTypeHeaderValue.Parse to parse the whole thing:

    content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/vnd.wolt.order+json;version=2beta1");