Reputation: 31
I'm working on a project, where in I need to get the data from server through RESTful web services.
Server side people have implemented a few web services. I need to use those methods, but I'm not sure which http method to use "Get" or "POST".
If I use "GET" as http method and if the server web service is being implemented in "POST" then I get 404 or similar http error code.
Is there any way to find out the http method type being implemented for a web server API?
Right now I'm using a BOOL flag to determine which Http method to use. The flag is being set from outside.
NSMutableURLRequest *urlRequest = nil;
if (_usePostMethod)
{
urlRequest = [[NSMutableURLRequest alloc] initWithURL:self.serverURL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:30.0f];;
[urlRequest setHTTPMethod:@"POST"];
[urlRequest setHTTPBody:[restMessage.message dataUsingEncoding:NSUTF8StringEncoding]];
}
else
{
urlRequest = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/%@", self.serverURL, restMessage.message]]
cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:30.0f];
[urlRequest setHTTPMethod:@"GET"];
}
I wanted to get rid of this BOOL _usePostMethod
variable.
Upvotes: 0
Views: 273
Reputation: 176
@Omkar Ramtekkar there is no any way to know ... before calling web services you must know about the http method which one you have to use either Post ot Get..
Upvotes: 1
Reputation: 2357
The GET
method means retrieve whatever information (in the form of an entity) is identified by the Request-URI. If the Request-URI refers to a data-producing process, it is the produced data which shall be returned as the entity in the response and not the source text of the process, unless that text happens to be the output of the process.
The POST
method is used to request that the origin server accept the entity enclosed in the request as a new subordinate of the resource identified by the Request-URI in the Request-Line. POST is designed to allow a uniform method to cover the following functions:
- Annotation of existing resources;
- Posting a message to a bulletin board, newsgroup, mailing list,
or similar group of articles;
- Providing a block of data, such as the result of submitting a
form, to a data-handling process;
- Extending a database through an append operation.
Upvotes: 0