How to not send a response body - php

I'm trying to send a header response back from our api with a http status code 201 Created and a Location:header.
No matter what I do I get a response body too, something that I don't want.
If I return an empty string (return "";), restler will put the string '""' in the response body. If I return null or do not return anything at all restler will put the string 'null' in the response body.
How do I tell Restler to not send anything but headers?

UPDATE :-
With the latest release of Restler 3 RC4. Returning null sends empty body for the response
This behaviour can be changed by setting
Defaults::$emptyBodyForNullResponse = false;
You can use #status comment to set the response code to 201
and #header comment for setting the location header
For older versions use the technique described below
From your api method, set both status and location header using header function followed by die or exit
header("HTTP/1.0 201 Created");
header('Location: http://api.example.com/item/45');
die();
This is a very valid use case that demands better way of doing this, We will soon update this answer with those solutions
Thanks for contributing to Restler :)

Related

php http post response for web hook

I'm trying to create a web hook notification. The documentation of the service i want to use requires that i specify a URL where POST requests can be performed. This URL will receive the following object, in json format, and must respond with a Status Code between 200-299.
{
"type": "ping"
}
I don't know how to proceed making my server on localhost respond with a 200 status code. http_response_code(200) works well on live server but nothing seem to be happening on localhost.
Is there any way i can make it work with localhost?
I've included the link to the documentation here (i hope it's not against the rule).
I am thinking that you wouldn't have to send them the response. The webhook would know about the response. If it reached your URL successfully, it would be a 200 OK right off the bat. If the API is requesting a response back then I imagine that you would have to call it back somehow. Is this a well-known API? Any documentation?
The response code is in the response header, not in the content.
PHP defaults to a response code of 200, so if you don't mess with it at all, you should be good.
If you want to set a different response code (202 for example), just call:
http_response_code(202);
Or set the full header yourself:
header('HTTP/1.1 202 Accepted');
Proper way to explicitly set 200 (or any other) status code with http_response_code function is just as following (don't echo or json_encode it):
http_response_code(200);
It should force webserver to use 200 status code in it's response. However, webserver could possibly ignore it. To check what response code your webserver sends, use telnet or any REST tool like Postman

Postman returns 404, while website returns 200

I have a Laravel sever serving an api at http://localhost:8000/api/v1_0/login.
This API when hit from the web page (a login form) works fine with 200 OK status, however when I try to hit the same API with Postman, it returns me 404. I'm sure that my headers and URL spellings are correct. Is this a bug? what else could be the reason?
Just in case someone still need an answer for this. I checked the answer posted by #ZI3n but the solution didn't worked for me.
Postman was receiving a String text\html which is a default Content-Type so I suspected the code I used to processed the JSON was, somehow, forming a string not recognized by Postman as a JSON. But when was received I could see it as JSON, if changed the body type to JSON.
Because of this the request was being received as JSON formatted text but was not recognized as a JSON content, and since I was expecting a JSON the status code was ignored and replaced with 404 Not Found. Not sure if this was by the server or Postman.
So I visited the URL directly on the browser, and it was obvious that the JSON was displayed but still read as text/html and not as application/json
The next step was to refactor the code again, this time I reprocessed the JSON string by assigning the string to a variable $json as follow:
//assigning previous json string to $json
$json = $originalEchoedJsonString;
//reprocessing the string a converting it back to object
$newJson = json_decode($json);
//preparing to be send
//if PHP version 5.4+
http_response_code(200);
// Any PHP version specially 5.3 and under
header('HTTP/1.1 200 Ok', true, 200);
header('Content-type: application/json; charset=UTF-8;');
//echoing JSON back with json_encode();
echo json_encode($newJson);
After doing this I sent the request with Postman again and voila, I got a 200 Ok status and the string was properly displayed as a JSON file. Also was recognized by the browser JSON formatter as a JSON.
I know this is a very old question, but for future people with this problem I'll add this.
On the Headers tab, there is a Host header option. If it is not checked then the server may not accept the request and return an error. In my case a 404 rather than the 400 shown in the postman tooltip below.
Curl and browsers do add it automatically, which is why they can behave differently when this option is not checked.

Catch POST response from API with 302 status

I am trying to catch POST response send to me by external API.
The problem is that POST array is completely empty while I can check in firebug that browser recieved it but with codes 302 FOUND and second (with same body) with code 307 TEMPORARY REDIRECT:
Is there any way to grab this data inside my script or is this something wrong with server re-directions?
If you are using the CURL library, there are two options that help with your case:
curl_setopt($curl,CURLOPT_HEADER,1);
This returns the response header including the status code. You can see whether 302 is returned.
Or you can simply follow the redirect
curl_setopt($curl,CURLOPT_FOLLOWLOCATION,1);
Edit: sorry just saw you were doing this on the client side.
If this is an AJAX call, you can get the status code in the raw XHR object.

Return a HTTP 200 Code to the POST client?

I have a server sending POST to me. I need to reply with HTTP 200 OK.
Server needs kind of like a "Go Ahead!" prompt before it executes another action.
It requires a HTTP 200 response.
EDIT
I've tried the header(), but the server for some reason won't read it?
The 200 code is a standard response to a successful request... Even echoing out an empty json string would result in a 200 OK status.
echo json_encode(array());
If all you want to do is signal to your client that some process was completed, you can just echo back a custom status message or even a blank object like I demonstrated above.
If you want to actually manually send the 200 header you can do so like this -
header('Status: 200');
Make sure that this header is send before you have any output from the server.
This function call does the job:
http_response_code(200);
See: http://php.net/manual/en/function.http-response-code.php
This function call can be thrown anywhere in the server code -- the order of when this function is called does not seem to matter.

Set Response Status Code [duplicate]

This question already has answers here:
PHP: How to send HTTP response code?
(8 answers)
Closed 6 years ago.
I have an API call for which I need to be able to run some checks and potentially return various status codes. I don't need custom views or anything, I just need to return the proper code. If the user hasn't passed proper credentials, I need to return a 401 status. If they haven't sent a supported request format, I need to return a 400 status.
Because it's an API, all I really want to do is set the response status and exit with a simple, stupid message about why the request failed (probably using a exit). Just enough to get the job done, but I haven't been able to get this to work right. I've tried using PHP's header() and Cake's $this->header() (this is all in the controller), but although I get the exit message, the header shows a 200 OK status.
Using the code below, I get the message, but the header isn't set. What am I missing?
if( !$this->auth_api() ) {
header( '401 Not Authorized' );
exit( 'Not authorized' );
}
PHP <=5.3
The header() function has a parameter for status code. If you specify it, the server will take care of it from there.
header('HTTP/1.1 401 Unauthorized', true, 401);
PHP >=5.4
See Gajus' answer: https://stackoverflow.com/a/14223222/362536
Since PHP 5.4 you can use http_response_code.
http_response_code(404);
This will take care of setting the proper HTTP headers.
If you are running PHP < 5.4 then you have two options:
Upgrade.
Use this http_response_code function implemented in PHP.
Why not using Cakes Response Class?
You can set the status code of the response simply by this:
$this->response->statusCode(200);
Then just render a file with the error message, which suits best with JSON.
I don't think you're setting the header correctly, try this:
header('HTTP/1.0 401 Unauthorized');
I had the same issue with CakePHP 2.0.1
I tried using
header( 'HTTP/1.1 400 BAD REQUEST' );
and
$this->header( 'HTTP/1.1 400 BAD REQUEST' );
However, neither of these solved my issue.
I did eventually resolve it by using
$this->header( 'HTTP/1.1 400: BAD REQUEST' );
After that, no errors or warning from php / CakePHP.
*edit: In the last $this->header function call, I put a colon (:) between the 400 and the description text of the error.
As written before, but for beginner like me don't forget to include the return.
$this->response->statusCode(200);
return $this->response;

Categories