Re: Sending PUT/DELETE request with data in 'Request Payload'

4,120 views
Skip to first unread message

Doug Neiner

unread,
Mar 11, 2013, 1:57:14 PM3/11/13
to ampl...@googlegroups.com
Hi Ray!

You will be happy to hear its a very easy fix. The problem is, by default, jQuery AJAX will send that data as key/value pairs using Form Data instead of the body content. So you just need to convert that data into a string so its posted as the content of the request instead of as key/value pairs. You can do that by adding a dataMap to the definition:

dataMap: function ( data ) {
   return JSON.stringify( data );
}

You can see a working example in this fiddle: http://jsfiddle.net/dougneiner/5AY6W/ (I use Mockjax to prevent actual requests from hitting the server )

Please let me know if you have any trouble getting this to work.

Sincerely,
Doug Neiner

On Sunday, March 10, 2013 9:03:27 PM UTC-5, RayK wrote:

I have a javascript object to be sent to server as follow :

var input = {a: 'aaa', b: 'bbb', c: 'ccc'};

And I want to send 'a' property in url like this

http://localhost/rest/customer/aaa

That's fine with url substitution feature in amplifyjs as follow :

amplify.request.define('update-customer', 'ajax', {
    url : 'rest/customer/{a}',
    dataType: 'json',
    type : 'PUT'
    contentType : 'application/json; charset=utf-8;
});

amplify.request('update-customer', { a : input.a, info : input });

The point I am struggling with is that I would like to send b and c property as a form data in json format as 'Request Payload', however it is failed becauseof the form data is sent as follow :

Request Payload :
  info : {b : 'bbb', c : 'ccc'}

So what I want to achieve is to remove 'info' key in 'Request Payload' as follow :

Request Payload :
 {b : 'bbb', c : 'ccc'}

I tested this in REST Client program and succeeded. This means I need to remove key attribute , 'info' in my case.

To wrap up my question, How to send data attached in Request Body without key name using amplifyjs? Thanks in advance.


Doug Neiner

unread,
Mar 12, 2013, 9:44:41 AM3/12/13
to ampl...@googlegroups.com
Hey Ray,

Elijah Manor and I were discussing this email, and he pointed out that I actually missed part of your code.

Amplify will automatically remove keys it matched in the URL replacement, so you should just need to change your line to this:
amplify.request('update-customer', input );

It will match the "a" and remove it, leaving just { b: 'bob', c: 'ccc' } to be sent to the server, without that extra "info" key you had.

Hope that helps!

Doug

--
You received this message because you are subscribed to the Google Groups "Amplify" group.
To unsubscribe from this group and stop receiving emails from it, send an email to amplifyjs+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

RayK

unread,
Mar 13, 2013, 11:59:59 PM3/13/13
to ampl...@googlegroups.com
Thank you so much Doug, it just works!

BTW, one more question on it, if I am going to include 'a' property in json message as well as including it in URL parameter, is there any easy option to accomplish it?

Ray

2013년 3월 12일 화요일 오후 10시 44분 41초 UTC+9, Doug Neiner 님의 말:

Doug Neiner

unread,
Mar 17, 2013, 11:45:18 PM3/17/13
to ampl...@googlegroups.com
Ray,

There is not a super easy way - you either have to change how the URL substitution pre filter works, or pass in an extra key. The extra key would be something like:

input._a = input.a;

and have 'rest/customer/{_a}' as your replacement variable, etc.

Doug

Scott González

unread,
Mar 18, 2013, 8:05:33 AM3/18/13
to ampl...@googlegroups.com
You can try using a dataMap to inject the extra key so that the caller doesn't need to provide it.

Doug Neiner

unread,
Mar 18, 2013, 3:30:03 PM3/18/13
to ampl...@googlegroups.com
Hey Scott!

How would using the data map work in this issue? The key is already removed from the data object by the time the data map prefilter has a chance to access the data. 

Doug

Scott González

unread,
Mar 18, 2013, 3:34:47 PM3/18/13
to ampl...@googlegroups.com
On Mon, Mar 18, 2013 at 3:30 PM, Doug Neiner <dne...@appendto.com> wrote:
The key is already removed from the data object by the time the data map prefilter has a chance to access the data.

That's silly. We should swap the order of the those subscriptions :-) 

Doug Neiner

unread,
Mar 18, 2013, 3:53:06 PM3/18/13
to ampl...@googlegroups.com
Haha! Yeah, it would be allow the developer to turn off/reorder the pre filters as well. 


Doug
--
Reply all
Reply to author
Forward
0 new messages