Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add parameters to query string when using PUT method with Angular's $http

I'm using Angular's $http service to make web api requests. When I use the GET method, the two param values are added to the query string:

// http://foo.com/api/test?heroId=123&power=Death+ray $http.get("/api/test", {    params: { heroId: 123, power : "Death ray" } }) 

However, when I use the PUT method the params are JSON-encoded and sent as the request payload:

// {"params":{"heroId":123,"power":"Death ray"}} $http.put("/api/test", {    params: { heroId: 123, power : "Death ray" } }) 

How can I force the params to be added to the query string when using PUT?

like image 656
tronman Avatar asked Jul 22 '15 20:07

tronman


People also ask

Can Put method have query parameters?

Is it OK to use query parameters in a PUT request? Absolutely. Query parameters are just another piece of the resource identifier.

Can we send parameters in PUT request?

You can send data to the server in the body of the HTTP PUT request. The type and size of data are not limited. But you must specify the data type in the Content-Type header and the data size in the Content-Length header fields. You can also post data to the server using URL parameters with a PUT request.

How do you pass query parameters in HTTP mule 4?

In Mule 4 in the Request section of the HTTP request connector in Mule 4 under the Query Parameters tab, we can specify the name and the value for the Mulesoft query parameters that we are going to send (Refer to Figure 1).

How are parameters sent in an HTTP PUT request?

Are parameters which are sent by PUT passed in the URL, or in the HTTP header ? Not the headers. It's the same as POST - either the URL or the body of the request. The only difference is the HTTP verb being used and of course the semantics that come with it (UPDATE a resource on the server).


1 Answers

With $http.put, $http.post or $http.patch, the config object containing your url parameters goes as the third argument, the second argument being the request body:

$http.put("/api/test",                                       // 1. url           {},                                                // 2. request body           { params: { heroId: 123, power : "Death ray" } }   // 3. config object ); 

$http.put documentation for reference

like image 131
Michael P. Bazos Avatar answered Oct 13 '22 02:10

Michael P. Bazos