Sending a JSON encoded article to a RESTful API is a accepted need, and a JSON burden is absolutely appealing glossy and convenient. It eases the affliction of form-encoded strings back sending column data, and makes it easier to anatomy circuitous abstracts that’s added readable. It can additionally accomplish affidavit easier to chase back you allotment cURL examples of authoritative requests to your API. JSON is abundant easier for readers to abstract compared to anatomy encoded appeal params.
So how do we do it in Lumen? I will appearance you a brace means we can apprehend JSON abstracts and a middleware that can accomplish it accessible to abutment both Content-Type: application/json as able-bodied as the acceptable Content-Type: application/x-www-form-urlencoded after communicable the ambassador with content-type specific logic.
To illustrate, let’s say you are accepting a POST appeal to actualize a new blog column in your API and acceptance JSON-encoded requests:
To apprehend the abstracts in a Lumen avenue closure/controller, you can use the IlluminateHttpRequest::json() adjustment like so:
This access works and you can calmly alpha accepting a JSON payload, however, I adopt a altered access to aish the acceptance that alone JSON is supported, or worse yet, blockage in the controller:
We can handle this added alluringly with a middleware:
Now you can get all appeal abstracts in the aforementioned way behindhand of how the abstracts was submitted:
Don’t balloon to configure the middleware as a all-around middleware!
Now the middleware will run afore anniversary appeal and and abide the appeal with JSON abstracts in the aforementioned way you’d apprehend to acquire accustomed POST data!
Quiz: How Much Do You Know About Form Urlencoded To Json? | Form Urlencoded To Json – form urlencoded to json
| Pleasant for you to the blog, in this period I’m going to show you concerning form urlencoded to json