RESTful API PHP Content Type - php

I am writing a RESTful API in the CakePHP framework. Using the POSTMAN app I am testing a delete method with the headers set as follows:
Content-Type application/json
I am sending my data in the body as RAW:
{"id":"8"}
MY API is routing the request as I would expect to my controller, and I'm reading the body with:
json_decode(file_get_contents('php://input'),'true');
This gives me empty data with the above parameters. However if I change the method type to POST or I remove the content type form the headers then I get content in the body.
Any ideas, seems like strange behaviour!

Related

Laravel get requests from multipart/form-data in POST method

I'm receiving third party request to my laravel post API with two form-data (ID, File) with headers (Content-type multipart/form-data)
In laravel controller, not getting requests in $request->all(), But could see the form-data in file_get_contents('php://input').
In the same request without content-type multipart-formdata, it's working fine, getting requests - id & file. (I've checked in Postman with same third party request params)
Content type as multipart-formdata is requesting from third-party, So couldn't remove or modify the headers. how to properly receive it in laravel using POST method?
Please guide.
Assuming your enctype="multipart/form-data" is correct and file name is myfile,
You should get it as
$file = $request->file('myfile');
in your controller or other class like FormRequest class or Validation class

Set form-data post parameter in Swagger inspector

I have just started swagger for documented api. I am using Swagger inspector for execute the api, before that I was using Postman. I am passing the data in Swagger body but not getting the data, same thing works in Postman but not working here.
Moreover get request works with Swagger but post is not working properly.
Getting response in Postman:
Same thing not working in Swagger:
I have already set the header, but not getting my parameter in post.
That's because you are not setting the Request Headers Content-Type in swagger, as you need to send the POST request you need to send the data via the Body tab and to send the data you need to specify the content type for the request body so do the following
Add a Content-Type : application/json header from the Authentication&Headers tab like below
if you are using any authentication headers for the request when using postman add them too
Now go to the Body tab
and add the following
{
"userName": "123",
}
That's all you need to do now click send and it will show you the data sent, just select any format for the request Headers and then provide the data in that format. Below are the supported formats

Could not submit form-data through postman put request

I m using lumen api 5.6.
This is my route
$router->PUT('collections/{collectionId}'
In postman i m calling this api like this.
and this is the body of that request
See here i have CollectioPoints here but its failing in validation.
the data i m sending through formdata is not able to recognized in validation.
But if i send the route from put to post [$router->POST('collections/{collectionId}'], all the data i m sending through form data are recognized and i m getting correct response as below
Why this is happening. Cant i send form data through put request?
Thaank you.
Laravel(Lumen) cheats because html forms only support GET and POST, but it does understand a real PUT/PATCH request.
In Postman You should send POST and set _method to PUT (same as sending forms) to make your data and files visible
_method = "PUT"
Or
If you don't have any file only have data then you can write you parameter in choose below option in Postman
x-www-form-urlencode
Or
You want use PUT method for send Data with form-Data option you should use stuff like this https://gist.github.com/devmycloud/df28012101fbc55d8de1737762b70348
Example
Headers: Content-Type application/x-www-form-urlencoded
_method = "PUT"
an your controller
return $request->file('avatar');
image example

Why do requests to my web API only succeed when Content-Type HTTP header is set?

I'm developing a REST-based web API with Yii2. Through testing, it seems that, in order to be successful, POST requests to the API need to have the Content-Type HTTP header set. (One tester was using Postman without setting the Content-Type header and was receiving 500 error responses in return, while another tester was using cURL and setting the Content-Type header and was receiving 200 success responses. When the Content-Type header is omitted, the POST data seems to be stripped from the request somewhere along the line, and no POST data gets logged by Yii for these requests.)
I was mostly following Yii's own guide for development and wasn't aware of any requirements in this area. Could someone explain why the Content-Type header must be set and what is happening otherwise?
I have discovered by looking in the Yii source code that there is a workaround.
When retrieving parameters that are submitted via POST (or other request methods) by calling the yii\web\Request::getBodyParam() method, the method checks if there is a parser set for the Content-Type of the request, but the method also checks if there is a parser set for Content-Type '*' (elseif (isset($this->parsers['*']))). So in my Yii application configuration, I just need to set the JsonParser for '*'...
'parsers' => [
'application/json' => 'yii\web\JsonParser',
'*' => 'yii\web\JsonParser'
]
and then I don't need to rely on clients having the Content-Type HTTP header set (as long as I handle the POST data sensibly).

"Unsupported media type" when PUTing to Apigility with Postman

I'm a building a RESTful API using Zend Framework 2 and Apigility by Zend Framework.
For testing, I use the chrome extension Postman REST-Client.
I can do GET requests and POST requests without problems by sending form-data without problems.
But when I try to do a PUT, PATCH or DELETE request, I get the following error:
{
"type":"http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html",
"title":"Unsupported Media Type",
"status":415,
"detail":"Invalid content-type specified"
}
Accept whitelist in Rest-Service-Config of Apigility:
application/vnd.timber-ms.v1+json, application/hal+json, application/json
Content-Type whitelist:
application/vnd.timber-ms.v1+json, application/json
The content-type of the response is application/problem+json
What can I do to fix this and do successfull PUT/PATCH requests?
Is this a problem with Postman or Apigility?
You're getting the 415 error of Unsupported Media Type when Apigility cannot deserialize the data coming from the client. This recently was called out in the documentation.
I suspect your problem is due to the content-type being sent from postman. Pay special attention to the Content-Type Whitelist listed for the service and make sure it contains the content-type you are sending.
For example, if your service has only has application/json in the Content-Type Whitelist and you send the PUT/PATCH with postman as x-www-form-urlencoded, you will get a 415 error of Unsupported Media Type. If you change postman to send the PUT/PATCH with a content-type of application/json and the request body contains valid JSON, Apigility should accept the request.
You can check the content-type postman is sending by clicking on the "Preview" button just to the right of the "Send" button.
I was having a similar issue in Postman with the unsupported media type responses. However, I experienced this response on both PUT and POST requests using my company's API.
I verified that Postman was the problem here and not the request/API in use by running the same request with another similar Chrome extension called Advanced REST Client. I'm not familiar with Apigility, but pretty sure that Postman is the culprit here.
Hoping there is a fix for this issue as Postman and its collections feature is much easier to use than that of Advanced REST Client.
on postman go to normal tab (or other auths tab) and add header:
Content-Type application/json (or any type you need)
make sure that on the raw tab include the json data
{
"type":"http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html",
"title":"Unsupported Media Type",
"status":415,
"detail":"Invalid content-type specified"
}
I had the same problem,
My solution was to write my data to [Body] in the [raw] in json format. Like this:
{"message": "UPDATED First Post!"}
And in [Headers] :
[KEY] Content-Type
[Value] application/json

Categories