Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should Rest API accept POST array or JSON string?

Tags:

json

rest

php

api

I've been reading a few REST tutorials and some of them says that in order to send data to a rest API you should send post data as an array, which is something like this:

$data = array('foo' => 'bar');
$rest->post($data);

Then there are others that say you should send JSON data like this:

$data = array('foo' => 'bar');
$data = json_encode($data);
$rest->post($data);

Not sure if there's a standard way of doing this or either if is fine but what is generally recommended when designing API's?

EDIT: There seems to be confusion. To clarify I agree JSON should be used for client consumption but this question is about SERVER consumption. Meaning should the SERVER accept JSON or POST data from its clients?

like image 731
IMB Avatar asked Aug 11 '12 20:08

IMB


People also ask

How do I send a JSON request from a REST API?

You also need to specify the data type in the body of the POST message using the Content-Type: application/json request header. In this REST API POST example, we also send the Accept: application/json request header to tell the REST API server that the API client expects JSON in response.

Does RESTful API send post or post data?

If you're the one creating the RESTful API, meaning your application is the server and is accepting requests, then your question seems confusing. Your application will not be sending any POST data; clients will be sending POST data to you.

Is it possible to make an API for JSON-encoded requests?

Your application will not be sending any POST data; clients will be sending POST data to you. With that being said, having an API which will accept JSON-encoded requests is possible, though would really only be useful for very niche scenarios.

What is REST API and how it works?

REST APIs work like a client-server architecture. The client makes a request and a server (REST API) responds back by providing some kind of data. A client can be any front-end framework like Angular, React, etc, or Spring application ( internal/external ) itself. Data can be sent in various formats like plain text, XML, JSON, etc.


1 Answers

If you're the one creating the RESTful API, meaning your application is the server and is accepting requests, then your question seems confusing. Your application will not be sending any POST data; clients will be sending POST data to you.

With that being said, having an API which will accept JSON-encoded requests is possible, though would really only be useful for very niche scenarios. The vast majority of clients will be POSTing data to your API in the application/x-www-form-urlencoded format. PHP handles this behind the scenes for you, and exposes the data as the $_POST superglobal.

If you're talking about responding to POST requests, and what format you should use, then that will depend on what format the client wants you to send it in. The client will either specify this in the Accept header, or some APIs allow it to be specified in the URL (e.g. foo.com/some/thing/123.json or foo.com/some/thing/123/json). The client isn't required to tell your application what format it wants, so it's up to you to pick a sensible default. Most APIs will use JSON these days.

I've never heard of an API that understood serialized PHP array format, though, so I don't know what resources you've been reading, but I'm pretty sure you misunderstood what they were suggesting.

like image 74
FtDRbwLXw6 Avatar answered Oct 19 '22 21:10

FtDRbwLXw6