1

I've done a lot of searching and nothing seems to fully address this. I've created a REST API that has a resource to send a message. The path is /api/v1/conversation/{type}/{id}/message. Placing a POST call to that URI will create a message for the given conversation.

Everything works great if I just use $.post('/api/v1/conversation/sample/sample/message', {message: "All your base are belong to us"});

However, I'd like to use Restangular, and for some reason, it is sending the POST data in a way that I have to work with request.body instead of request.POST.get('message'). This is terribly inconvenient if I have to do this with every single server side API.

Here's my Restangular code:

conversation = Restangular.one('conversation', scope.type).one(scope.type_id);
conversation.post('message', {message: "All your base..."})

To clarify, it is POSTing to the correct URI, it just is sending the post data as a payload instead of as form data. How can I configure it to send the post as form data?

Edit: As a side note, I was able to mitigate this issue by creating a utility function:

def api_fetch_post(request):
    post = request.POST
    if not post:
        try:
            post = json.loads(request.body.decode(encoding='UTF-8'))
        except:
            pass
    return post

This way I can accept either type of POST data. Regardless, is there a way to send form data with Restangular?

1
  • How did you solve this by Restangular. Because the below solution is now working. Commented Jun 6, 2014 at 5:28

1 Answer 1

2

Yes, there is.

var formData = new FormData();
formData.append('message', $scope.message);
// Or use the form element and have formData = new FormData(formElement).

Restangular.one('conversation', $scope.type).one($scope.type_id)
       .withHttpConfig({transformRequest: angular.identity})
       .post(formData, null, {'Content-Type': undefined})
       .then(function(response){
           // Do something with response.                    
});
Sign up to request clarification or add additional context in comments.

1 Comment

I use formdata with post and it appends object object to url that gives me 404. So i tried it with customPOST but turns out with same django can't parse request since it sends some weird webkit content disposition etc. Grrr.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.