2

I am using HTML to download xlsx,pdf files using POST call. I am using the below code to send my payload but it is sending as url format,

 <form method="post" action="{{url}}" enctype='application/json'>
<input type="text" name="type" value="{{type}}" hidden>
<input type="text" name="paramValue" value="{{value}}" hidden>
</form>


Request body
type=Shop&paramValue=Kumar

I need to pass it as JSON because of this I am getting an error as,

Response    HTTP/1.1 415 Unsupported Media Type

Please suggest how to pass the data as JSON in case of using form in HTML.

user3428736
  • 864
  • 2
  • 13
  • 33

1 Answers1

1

Form enctype attribute does not support application/json as seen here.

You need to manually json encode your data to use with your post request or use the $http service which posts json by default:

var data = { type: $scope.form.type, value: $scope.form.value };

$http.post(url, data) // default content-type is 'application/json' for $http.post
.success(function(data, status) {
  //yay
});
tommybananas
  • 5,718
  • 1
  • 28
  • 48
  • Using $http it is a seperate Angular call, I can't use it with form action, where it required to pass the url with $http, but I need to construct JSON in form and action. Please suggest how to achieve this. – user3428736 Nov 08 '16 at 08:59
  • You cannot do it with a plain form. You need JavaScript to serialize into Json and set request header content-type. That said, you can use onsubmit or onclick with angular to call into your controller or use jquery, but it is impossible with vanilla HTML forms – tommybananas Nov 08 '16 at 09:03