웹 API 서비스에 POST에서 json을 보내는 동안 오류가 발생했습니다.
Web API를 사용하여 웹 서비스를 만들고 있습니다. 간단한 수업을 구현했습니다.
public class ActivityResult
{
public String code;
public int indexValue;
public int primaryCodeReference;
}
그런 다음 컨트롤러 내부에 구현했습니다.
[HttpPost]
public HttpResponseMessage Post(ActivityResult ar)
{
return new HttpResponseMessage(HttpStatusCode.OK);
}
그러나 POST에서 json 파일을 전달하는 API를 호출하면 다음과 같습니다.
{"code":"XXX-542","indexValue":"3","primaryCodeReference":"7"}
다음과 같은 오류 메시지가 나타납니다.
{
"Message": "The request entity's media type 'text/plain' is not supported for this resource.",
"ExceptionMessage": "No MediaTypeFormatter is available to read an object of type 'ActivityResult' from content with media type 'text/plain'.",
"ExceptionType": "System.Net.Http.UnsupportedMediaTypeException",
"StackTrace": " in System.Net.Http.HttpContentExtensions.ReadAsAsync[T](HttpContent content, Type type, IEnumerable`1 formatters, IFormatterLogger formatterLogger, CancellationToken cancellationToken)\r\n in System.Net.Http.HttpContentExtensions.ReadAsAsync(HttpContent content, Type type, IEnumerable`1 formatters, IFormatterLogger formatterLogger, CancellationToken cancellationToken)\r\n in System.Web.Http.ModelBinding.FormatterParameterBinding.ReadContentAsync(HttpRequestMessage request, Type type, IEnumerable`1 formatters, IFormatterLogger formatterLogger, CancellationToken cancellationToken)"
}
내가 뭘 잘못하고 있죠?
HTTP 요청에서 Content-Type을 다음과 같이 설정해야합니다. Content-Type: application/json
따라서 피들러 클라이언트를 사용 Content-Type: application/json
하는 경우 요청 헤더에 추가하십시오.
또 다른 팁 ... "content-type : application / json"...을 Composer / Parsed 탭의 텍스트 상자 필드에 추가하는 곳입니다. 이미 3 줄이 채워져 있으므로이 Content-type을 4 번째 줄로 추가했습니다. 그것이 포스트를 작동하게했습니다.
- 헤더 속성을 추가해야합니다.
Content-Type:application/json
당신은 주석이되어야 모든 POST 요청 방법 입력 매개 변수를 정의 할 때
[FromBody]
, 예를 :[HttpPost] public HttpResponseMessage Post([FromBody]ActivityResult ar) { return new HttpResponseMessage(HttpStatusCode.OK); }
Any JSON input data must be raw data.
Please check if you are were passing method as POST
instead as GET
. if so you will get same error as a you posted above.
$http({
method: 'GET',
The request entity's media type 'text/plain' is not supported for this resource.
I had all my settings covered in the accepted answer. The problem I had was that I was trying to update the Entity Framework entity type "Task" like:
public IHttpActionResult Post(Task task)
What worked for me was to create my own entity "DTOTask" like:
public IHttpActionResult Post(DTOTask task)
It require to include Content-Type:application/json
in web api request header section when not mention any content then by default it is Content-Type:text/plain
passes to request.
Best way to test api on postman tool.
참고URL : https://stackoverflow.com/questions/22384354/error-sending-json-in-post-to-web-api-service
'programing' 카테고리의 다른 글
Rails가 jQuery에서 JSON을 올바르게 디코딩하지 않음 (정수 키가있는 해시 배열) (0) | 2020.09.07 |
---|---|
tmuxinator 프로젝트에서 창 비율 지정 (0) | 2020.09.07 |
INotifyPropertyChanged를 구현할 때 [CallerMemberName]이 대안에 비해 느립니까? (0) | 2020.09.07 |
std :: string을 NSString으로 변환하는 방법? (0) | 2020.09.07 |
matplotlib : 축 오프셋 값을 정수 또는 특정 숫자로 포맷 (0) | 2020.09.07 |