Problem Statement:
I'm trying to post the data to the test url in C# using JSON data is failing, but when i try the same thing in Postman it succeeds.
C# Code snippet
string uploadPath = @"https://api.test.com/test";
string jsonData = "{ \"message\":\"ERROR: ABCDEXY: Price\"," +
"\"source\":\"BYODB\"," +
"\"tag\":[\"ABXT\",\"I232-F103\"],\"ID\":\"{76573406E8}\"}";
using (var client = new HttpClient())
{
client.DefaultRequestHeaders.Add("Authorization", "apiKey " + "7cbafstad-677654c4-8765fgt-95deb");
var content= new StringContent(jsonData, Encoding.UTF8, "application/json");
content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
HttpResponseMessage response = client.PostAsync(uploadPath, content).Result;
var responseBody = response.Content.ReadAsStringAsync().Result;
if (response.IsSuccessStatusCode)
{
var sucessRes = JsonConvert.DeserializeObject<dynamic>(responseBody);
//Print Success Msg
}
else
{
var failureRes = JsonConvert.DeserializeObject<dynamic>(responseBody);
//Print Failure Msg
}
}
Exception Details:
For Response Object, i'm receiving:
response = {StatusCode: 400, ReasonPhrase: 'Bad Request', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
{
Transfer-Encoding: chunked
Connection: keep-alive
X-Response-Time: 0.001
X-Request-ID: 4514d1b-1a3f-4277-9997-2813cd9d28ed
X-Rat...
For Response Body, i'm receiving:
{"message":"Invalid JSON","took":0.001,"requestId":"4514d1b-1a3f-4277-9997-2813cd9d28ed"}
When i try to invoke this through postman,it is succeeding:
What i'm doing wrong in my C# JSON Post..?
\r\nto\\r\\nit may well fix the problem, but I agree with Janothan Alfaro that it would be better not to try to hand-craft the JSON.