Resolved Empty body http client.

Marcos Santos

Member
Joined
Nov 11, 2021
Messages
11
Programming Experience
1-3
Hello guys,

I am in trouble with a problem. I want to send a post to a rest api and it does not need a body or require.

with Java, I was able to use: var body = RequestBody.create(null, new byte[]{});

I was able to create an empty body passing => null, new byte[]{} as parameters

if I use var body = null (in java), gives me an error.

I am building the same app now but in c#

C#:
HttpClient client = new HttpClient();
Headers.addHeaders(client, "my_url", confirmationList[I].Region, confirmationList[I].Token);

HttpResponseMessage response = client.PostAsync("/post", null).Result;

if (response.IsSuccessStatusCode)
{

    string result = response.Content.ReadAsStringAsync().Result;

    int code = (int)response.StatusCode;

    Console.WriteLine("Result code: " + code + " / result -> " + result);

}
else
{
    Console.WriteLine("Error code: {0, Response: ({1})", (int)response.StatusCode, response.ReasonPhrase);
}

If I pass null result or any kind of HttpContent like StringContent, it returns this error:

System.FormatException: Input string was not in a correct format.
at System.Text.StringBuilder.FormatError()
at System.Text.StringBuilder.AppendFormatHelper(IFormatProvider provider, String format, ParamsArray args)
at System.String.FormatHelper(IFormatProvider provider, String format, ParamsArray args)
at System.IO.TextWriter.WriteLine(String format, Object arg0, Object arg1)
at System.IO.TextWriter.SyncTextWriter.WriteLine(String format, Object arg0, Object arg1)
at System.Console.WriteLine(String format, Object arg0, Object arg1)

But the problem is, the server requires an empty BODY I guess, I tried everything and nothing works! with Java I was able to manager with that line I posted.

If anyone knows the solution, how to send an empty json body
 
Last edited by a moderator:
Back
Top Bottom