I use the ASIHttpRequest in my ios project, and my workmate use the Tastypie as the server, he told me I must post a String with JSON format as Http body(use application/json), but I can't find this Content-Type besides "application/x-www-form-urlencoded" and "multipart/form-data".
then How can I send a POST request with JSON format in ASIHttpRequest?
Related
Can I add raw data to the Post URL Request?
for example:
HTTP://token.abc/v1/oatuh2/rest/token{"OrgID: ABC", "sport:basketball"}
image below is from postman for understanding
Why Authorization header is mostly used to send a bearer token to server? Why don't we send our authorization token as URL parameter or post it as json payload with the request body?
Headers are perfect to hold these data, they are independent of request type.
You could send Authorization token in body, even everything other like Content-Type, Content-Length, cache headers also but different request types (POST,GET..) could have different request body format. GET sends data using query parameters POST/PUT in encoded form in the body (with Content-Type: application/x-www-form-urlencoded to make server aware of incomming data format), Content-Type: application/json with JSON in body, XML and others. Things get more complicated on multipart requests (check this https://stackoverflow.com/a/19712083/1017363).
So as you can see authorization token in body or query makes things more complicated on client and server side. Client should know how to "fit" authorization token on every request and server should know then how to read this value.
I need to parse a request body as JSON, but how do I check if the request is a JSON request?
Have you tried request.content_type?
But the response could be a plain text and still be a valid JSON, so I think it would be valid to get the contents and try to parse it.
I am designing a RESTful API when I noticed something strange.
When I make a POST request for creating a new record, the form data is sent in request payload.
But when I make a PUT request to update a record, it appends form data in the URL, very similar to GET request.
Now a URL has certain length limit. So what would happen if PUT request has larger data than this limit.
Will the PUT request fail?
Is it unsafe to use PUT instead of POST to update a record having large form data?
EDIT:
I am using NodeJS server. I am using restangular(angular framework) to build my PUT request.
Use customPUT to send the form data in payload.
baseObj.customPUT(newObj).then(function(xyz){})
Have a look at these threads
Can HTTP PUT request have application/x-www-form-urlencoded as the Content-Type?
PHP multipart form data PUT request?
application/x-www-form-urlencoded or multipart/form-data?
Sounds like you can basically set a Content-type: multipart/form-data header and be golden. Basically comes down to configuration of the request with restangular and support thereof on the NodeJS server.
I am able to test the web services by setting Content-Type : "application/json" and passing parameters or composing body, for ex: {"name":"test"}, using fiddler. But, how to compose request for XML content type for a POST method.
Presumably, you'd set the Content-Type to application/xml and use XML to format the request body. e.g. test although the format would depend on what the API expects.