user3533910
user3533910

Reputation: 37

Problems with PostAsJsonAsync method

I'm scratching my head with a problem, the method PostAsJsonAsync returns a 500 internal server error, but the JSON that the method is trying to send is correct since when I use Postman I send the same JSON and the server receives it, I read all questions I found about the subject and I can't find where the heck is my problem, I'm running as localhost, here is the code snippet:

client.BaseAddress = new Uri(endPoint);
client.DefaultRequestHeaders.Clear();
client.DefaultRequestHeaders.TryAddWithoutValidation("ZUMO-API-VERSION",2.0.0");
client.DefaultRequestHeaders.TryAddWithoutValidation("RM-EXT-KEY", key);

var content  = new StringContent(JsonConvert.SerializeObject(NotifiedUser), 
                   Encoding.UTF8, "application/json");
var response = await client.PostAsJsonAsync(endPoint, content);

I already checked and TryAddWithoutValidation is returning true for both instances so every parameter for the PostAsJsonAsync method are correct.

Thank you!

Upvotes: 0

Views: 2266

Answers (1)

Jack0fshad0ws
Jack0fshad0ws

Reputation: 573

Have you tried just client.SendAsync instead of client.PostAsJsonAsync(endPoint, content)? I had a similar issue talking to external java services and PostAsJson returned 400, but when changed to:

var request2 = new HttpRequestMessage(HttpMethod.Post, uri)
{
    Content = new StringContent(JsonConvert.SerializeObject(apdRequest), Encoding.UTF8,
        "application/json")
};

var response = client.SendAsync(request2).Result;

it started working...

It appears to be something where PostAsJsonAsync is not doing the serialization correctly -- but is not giving any indication of a problem.

Upvotes: 1

Related Questions