I'm developing a REST API using Zend Framework 1.12.3. I would like to know whether it's possible to set a HTTP response code from inside a Handler.
I'm using the Handler to check the "Accept" header. In case the requested format type is not supported, I should set a 415 HTTP error (Unsupported Media Type). However, I'm not able to set a response code from inside the Handler.
What do you mean by handler?
You can set a response code anywhere you have access to the Response object.
Technically, you can access the Response object nearly anywhere (after Bootstrap, at least) using:
$response = Zend_Controller_Front::getInstance()->getResponse();
The set your response code using:
$response->setHttpResponseCode($code);
It's most natural to do this in controllers since each controller already has a reference to the Response object:
$this->_response
Related
I am using install4j and I want to call rest api's from installer.
I have searched documentation for HTTP request action but not able to find, so my problem is,
I want to sent json with http request using post method, for that I have provided following properties:
Request Headers : Content-Type=application/json,
Form data = key1=value1;key2=value2;....keyN=valueN,
URL=http://localhost:8180/ng/app-setting
But I am not understanding, how to provide authentication details?.
When I run installer file http request execution is start and ask username password after that error message displayed which I was set in properties, But how installer provide exact error message? Because I am not getting what is exact error or what is response message/code?
Please provide the solution and documentation links.
Also let me know the steps how to provide http request (with REST) POST method with json.
how to provide authentication details
To set the authentication programmatically, set the system properties serverAuthUser and serverAuthPassword in the installer.
how to provide http request (with REST) POST method with json.
Currently this is not possible. I've added this to our issue tracker.
Update 2017-11-13
As of 6.1, when you set the "HTTP request method" property of the "HTTP request" action to "POST", a "Custom request body" child property is shown. If selected, the "Content type" and "Request body" properties can be configured.
I am using PostMan as a REST client to test this API method Cisco ACL Analysis API. specifically POST /acl/trace or getAClTracksStd (first go to Policy Analysis)
Here is my PostMan HTTP test call
Does anyone who is familiar with PostMan understand why I am getting this "Request method 'GET' is not supported" error from the server? I am making a POST HTTP request, not GET.(Selected from Drop down menu) It make more sense for me to get a input invalid parameter error or something.
Just to show that the endpoint url works, heres a HTTP test request that works
(same link, host->host API -> GET /host/{startIndex}/{recordsToReturn}
There's two issues that I'm seeing with your REST call. First, the error you're seeing is because the call needs to be preceded by "https://". Second, remove the interface IDs parameter and values. You should get a response with data after making these changes.
Your json looks erronuous (comma after the destIp) - and the server probably always responds with a default confusing error message in this case. (Postman is very well tested and it sends POST).
I was trying to follow along with one of the Dart HttpServer examples from GitHub, and, while it does show how to create and handle routes, I'm still at a loss on how to produce and send an HttpResponce in response to a specific url being requested.
For example, while working with WebAPI, I would have an Api Controller, in which I could explicitly define an action for GET, PUT, DELETE and POST verbs, and return an appropriate HttpResponse with a resource from each such method.
Does anyone know how this typical CRUD business is done using Dart as an Http server?
Once you receive a HttpRequest you need to use the response attribute to answer.
void sendOk(HttpRequest request, [content]) {
request.response
..statusCode = HttpStatus.OK
..write(content)
..close()
;
}
Can anyone tell me how to send a file to server and a username. I tried to set HTTP body with the content of the file and use GET method to set username but it doesn't work and encounter the exact situation as this post:( this is a bug) :
NSURLRequest cannot handle HTTP body when method is not POST?
As the post says, can anyone show me how to use PUT method as in both the ios client and php server or show me another method ?
This post if a follow-up question to mt previous post:
Android RESTful Web application using Zend Framework
I have written a web application which is based on the Zend Framework (Version 1.11.11) and I want to use the SAME backend code for coding the mobile version of this application (Android). To achieve this, I want to get the response for each of the actions in the controllers in XML and JSON - for mobile-based app.
Using the answers provided in the above link, I am able to get the XML and JSON response by making use of the AjaxContext helper. I set the contexts as json for the required actions in the init method and I am getting the desired json response.
Now I have another challenge. How to know from the URL if the given action was a GET or a POST request? Do I have have to add that as a query parameter? Is that the correct way?
For example, for login action within User controller: URL will be: [link] (http://localhost/user/login)
But within the loginAction, I check if the given request if a post and authenticate only if the request is a post. So the URL: http://localhost/user/login?format=xml will always return to me the response for a GET request since there is no way of knowing if the request was a GET or POST. Please help.
Thanks.
Abhilash
like you added format parameter do the same for request . Use "method" parameter to decide what type of request is it . Or you can also do is
$this->getRequest()->isPost(); //create
$this->getRequest()->isGet(); //read
$this->getRequest()->isPut(); // update
$this->getRequest()->isDelete(); // delete