How to POST content as application/x-www-form-urlencoded

Wolfman Joe

angularjs $http.post is refusing to use my Content-Type

I am working with a contractor - their team are making server-side APIs while I'm putting together a javascript application using angularjs. They insist on making the api only allow calls with application/x-www-form-urlencoded calls, so I'm trying to figure out how to use $http to make a urlencoded call, and running into problems. All the instruction pages I'm finding seem focused on older versions of angularjs.

I try using the below code:

$scope.makeApiCall = function(  ){
    var apiData = {
                    "key1" : "value1",
                    "key2" : "value2"
                };
    var apiConfig = {
                    "headers" : {
                        "Content-Type" : "application/x-www-form-urlencoded;charset=utf-8;"
                    }
                };
    return $http.post('/Plugins/apiCall', apiData, apiConfig)
    .then(function(response){
        $scope.data=response.data;
    });
};

But when I make the call, instead of using the Content-Type I provided, the developer tools report that it uses Content-Type: text/html; charset=utf-8

How do I get my $http.post to send the right Content-Type?

georgeawg

How to POST content as application/x-www-form-urlencoded

Use the $httpParamSerializerJQLike service to transform the data:

.controller(function($http, $httpParamSerializerJQLike) {
  //...

  $http({
    url: myUrl,
    method: 'POST',
    data: $httpParamSerializerJQLike(myData),
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    }
  });

});

For more information, see

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to send post request with x-www-form-urlencoded body

How to get and parse JSON response from x-www-form-urlencoded POST, RestTemplate (Java)?

How to force Angular2 to POST using x-www-form-urlencoded

How to perform a GET request with application/x-www-form-urlencoded content-type in Go?

How to get response of content type application/x-www-form-urlencoded by passing parameters in laravel

Retrofit + POST method + www-form-urlencoded

RestSharp defaulting Content-Type to application/x-www-form-urlencoded on POST

how to post data in node.js with content type ='application/x-www-form-urlencoded'

How do I post data using okhttp library with content type x-www-form-urlencoded?

Post 'x-www-form-urlencoded' content with aurelia-fetch-client

Swift - How to send POST request with "x-www-form-urlencoded" content-type

my request failed when the post 'content-type' is application/x-www-form-urlencoded and * form field param= { <this is a json object>}

How to POST x-www-form-urlencoded in retrofit

How to send a POST request with Content-Type "application/x-www-form-urlencoded"

How to post request with spring boot web-client for Form data for content type application/x-www-form-urlencoded

How to send post request with content-type x-www-form-urlencoded android retrofit

angular http post with 'Content-Type': 'application/x-www-form-urlencoded' and responseType: 'text'

Angularjs $http.post to contact form 7 with content type application/x-www-form-urlencoded

Akka HTTP how to POST singleRequest with Content-Type application/x-www-form-urlencoded

How to escape + in post call content type as application/x-www-form-urlencoded

How test Post request with custom object in content type application/x-www-form-urlencoded?

How to send post request with x-www-form-urlencoded body with loopj-async-http library

How to convert x-www-form-urlencoded post Message to JSON post Message?

How to send x-www-form-urlencoded in a post request in webclient?

How to solve error at xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded")?

how to post body x-www-form-urlencoded using webclient?

How to do a post request using FORM-DATA or x-www-form-urlencoded with Android?

How to add "application/x-www-form-urlencoded" as Content-type in .Net httpClient

How to do a x-www-form-urlencoded POST login using cypress?