0

I am using angular-restmodto send POST requests to a Django REST Framework backend. But whenever a string containing ? will result in a broken POST data (when received): For instance, suppose I am sending POST data

{"properties": "values", "status": "?a=3", "other properties": "other values"}

the backend will receive request.data

MergeDict(<QueryDict: {u'{"properties": "values", "status": "?a': [u'3", "other properties": "other values"']}>, <MultiValueDict: {}>)

where things starts to fall apart at =.

PUT methods works fine in these kind of cases. I wonder how to fix this?

========== EDIT ========== In the end I found an old setting for $http:

# $httpProvider.defaults.headers.post["Content-Type"] = "application/x-www-form-urlencoded"

which overwrites the default JSON content type of restmod. I removed it, and the problem is solved.

1 Answer 1

0

While I do not know exactly how anuglar-restmod works, it looks like you are trying to send JSON-like data, but it's actually being sent as form-encoded data. When data is form-encoded, it is combined to form key1=val1[&key2=val2...] and sent as one long string in the request body.

So while you were expecting the JSON data to be sent as

{"properties": "values", "status": "?a=3", "other properties": "other values"}

It was really being sent as form-encoded data, taking the form of

{"properties": "values", "status": "?a=3", "other properties": "other values"

Which should make it somewhat obvious what your issue actually is: It's not actually being form-encoded. This is being broken up such that {"properties": "values", "status": "?a is the key and 3", "other properties": "other values" is the value.

So part of this may be the fault of the tools you are using, but there is one easy way to fix it: make sure you are using JSON.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. Indeed Chrome shows that POST data are sent in form data. Let me check how to modify it.

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.