Home > Mobile >  Rest api: how can i create an api with Put method?
Rest api: how can i create an api with Put method?

Time:09-17

Hy every one, Till now i used post method to update the records on the servers, and never user Put method for that purpose because i am in big confusion of how to send and receive data on Put request. I researched on some blogs but i was unable to undrerstand from it.

I have following questions:

Question 1: Did i have to use param or form-data or both in Put request?

Question 2: Also, can we share data in both param and form-data in any Requests?

Question 3: imaging a sitation that i have to update an record in server with some basic info and an attachment along with it.so how i will go for it with Put method?

Currently i am creating api's in codeigniter 3.

Please help me out on that and Thanks for your attandence.

CodePudding user response:

Question 1: Did i have to use param or form-data or both in Put request?

You can use both simultaneously.

Params in URL can be read using $this->input->get() while for PUT, you can use $this->input->input_stream('key', TRUE); or $this->input->raw_input_stream see documentation

You will have to parse or process the raw data before you can actually use it in the code. To see what the PUT data looks like, just var_dump it

Question 2: Also, can we share data in both param and form-data in any Requests?

It is better to use separately. But it depends on you requirement, if you wish to do that, you may use array_merge() to merge GET and parsed PUT data into one variable

Question 3: imaging a sitation that i have to update an record in server with some basic info and an attachment along with it.so how i will go for it with Put method?

You may put basic info in URL params and file content in request body, but it may look ugly and URL has length limit so it may not suitable if your basic info is big.

Or just format them as raw JSON in request body. Example:

{
    "name": "John Smith",
    "address": "Blablabla",
    "certificate_file": "<base64 encoded file content>"
}

And for the server side code, like what I had commented before, I'd suggest you to use publicly available library such as CodeIgniter Rest Server

  • Related