TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
prasanna p
1.3k
470
105.2k
Web API : always showing message content type must be application/json
Sep 3 2020 6:52 AM
Hi friends,
I am trying to get the data from API using post method.It has Http headers and json parameters.
Tested API in the postman it is working.
In the development, the response status is showing success and status code is 200 (ok) but the responsemessage always showing content type must be application/json.I tested with diiferent ways to get the data.
Please check the following code :
HttpClient client =
new
HttpClient();
client.BaseAddress =
new
Uri(
"apiurl"
);
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.TryAddWithoutValidation(
"Content-Type"
,
"application/json"
);
client.DefaultRequestHeaders.TryAddWithoutValidation(
"Cache-control"
,
"no-cache"
);
var json =
new
person() { fromdate =
"01/08/2020"
, todate =
"20/08/2020"
, locationcode=
"1234"
, accessKey=
"12345"
};
var request =
new
HttpRequestMessage(HttpMethod.Post,
"apiurl"
)
{
Content =
new
StringContent(JsonConvert.SerializeObject(json), Encoding.UTF8,
"application/json"
)
};
var response = client.SendAsync(request).Result;
if
(response.ReasonPhrase ==
"OK"
)
{
var responseContent = await response.Content.ReadAsStringAsync().ConfigureAwait(
false
);
}
I added header and parameters like this also
client.DefaultRequestHeaders.Accept.Add(
new
MediaTypeWithQualityHeaderValue(
"application/json"
));
I tried by adding headers to request also but result is not getting fron the API.
request.Content =
new
StringContent(
"{\"fromdate\":\"01/08/2020\",\"todate\":\"20/08/2020\",\"locationcode\":\"1234\",\"accessKey\":\"12345\"}"
,
Encoding.UTF8,
"application/json"
);
Always showing content type must be application/json.
Please help me on this how to get the data from API.
Reply
Answers (
4
)
Populate bootstrap dropdown from xml
compare csv records in database and add new line