How do I get _all_ the NSURLRequest data? - ios

I'm going to be using a different protocol, so probably implementing a NSURLProtocol handler. Anyway, the server still basically wants a HTTP request. Which I have already when I'm talking HTTP.
I suppose I can serialize it but the code must be out there somewhere...just not sure where. Anyone know?
Eg: I want this....from a NSURLRequest
POST /some/control/endpoint HTTP/1.1
Header: Blah blah
Header2: Foo Bar
[Body data here]
Thanks

If you're asking how to construct the request, it is basically:
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:...];
[request addValue:#"Blah blah" forHTTPHeaderField: #"Header"];
[request addValue:#"Foo Bar" forHTTPHeaderField: #"Header2"];
request.HTTPMethod = #"POST";
Then create an upload task and provide the body data. Note that you'll also probably have to include Content-Length and Content-Type headers, too.
But if you're asking how to get the data in the format you show above from a request, you pretty much have to construct it yourself with string concatenation. Remember that you need to send \r\n at the end of each header line to be spec-compliant.
Or if you need a fully compliant protocol implementation, it might be simpler to create yourself a local listen socket, point NSURLSession at it, capture the request, forward it, capture the response, and forward it back.

Related

AFNetworking 2.0 how to put some parameters in the query string of a POST request

I need to call a ws to send an image to a server. Who mades the webserver, decided that the session token should be sent trough the URL and not in the body.
The query should look like: http://api.service.com/imageupload.php?token=434353435 and in the body there should be the image with its parameter.
The session manager when I use the -POST methods puts all the parameter inside the body.
Is there a way to say to the request serializer to put some parameters in the query string and others in the body? or do I need to subclass it?
This can be done like this
NSURLRequest * request = [NSURLRequest requestWithURL:[NSURL URLWithString:[#"Google.com" stringByAppendingString:AFQueryStringFromParametersWithEncoding(parameters, NSUTF8StringEncoding)]]] ;
Where parameters is a dictionary

Set the header in ASIHTTPRequest

I'm using ASIHTTPRequest to access a web based API and need to set a header for App authentication. Note that this is not a server level authentication it is at API level. I've tried every thing I could find and most of the answers on the web as well as the ones here at www.stackoverflow.com tell me to use something like:
[request addRequestHeader:#"username" value:#"asdf"];
This does not work for me. The guy who built the API I'm using told me that I need to set the header as:
Authorization: TRUEREST username=PersonName&password=pass&apikey=dfiu6aewruif3Bismillah4Rah3anArahimiImi22MyDad
So I tried the following:
NSURL *url = [NSURL URLWithString:#"http://ifish-uk.co.uk/rest_users/login.json"];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request addRequestHeader:#"username" value:#"MyUser"];
[request addRequestHeader:#"password" value:#"MyPass"];
[request addRequestHeader:#"apikey" value:#"dfiu6aewruif3Bismillah4Rah3anArahimiImi22MyDad"];
But it didn't work... I even tried setting the Request type to GET because the developer told me I should do this:
[request setRequestMethod:#"GET"];
This didn't work... The API developer told me he is made this module as follow:
POST /rest_catches/add.json HTTP/1.1
Host: ifish-uk.co.uk
Authorization: TRUEREST username=MyUser&password=MyPass&apikey=dfiu6aewruif3Bismillah4Rah3anArahimiImi22MyDad
Cache-Control: no-cache
any help would be greatly appreciated.
You should add only one header is Authorization no need to add separate headers for each field (use, pass, etc).
Fill it with your specific values and send.
[request addRequestHeader:#"Authorization" value:#"TRUEREST username=PersonName&password=pass&apikey=dfiu6aewruif3Bismillah4Rah3anArahimiImi22MyDad"];
Did you try adding:
[request setRequestMethod:#"POST"];
?

Http Post Example for xcode

I need a simple example where we POST some HTTP request to the server. And also i need to add some headers to that HTTP post. I have tried posting the HTTP post, but little confused about adding headers in the request. Is there any mandatory field/HEADER i have to add in the HTTP Post ??
Suppose you have an NSURLRequest called theRequest.
You configure the request type to POST with
[theRequest setHTTPMethod:#"POST"];
And for the headers, you add values to the Header you add them as key value pairs:
[theRequest setValue:authorizationString forKey:#"Authorization"];
which sets authorizationString for the Authorization header.

Using accept header with NSMutableURLRequest on iOS

I have a problem with my NSMutableURLRequest. My server supports both JSON and XML formats and they are separated with an access header. It also defaults to JSON if no access header is set.
Which basically means that when I want the response in XML I need to create a request with 'application/xml' as access header.
The problem I'm facing now is that even if I pass in the correct access header to get the response in XML I still end up with JSON (because that's default). It's like my request disregards the access header. Is there anything else I need to create in order to make my request work with headers?
The request is really simple:
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setValue:#"application/xml" forHTTPHeaderField:#"Accept"];
I have confirmed that my request contains my headers by printing allHTTPHeaderFields:
headers: {
Accept = "application/xml";
}
May be use should provide Content-Type?
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setValue:#"application/xml" forHTTPHeaderField:#"Content-Type"];
Because "application/xml" is always passed in "Content-Type" header field.

How to write data to the web server from iPhone application?

I am looking forward for posting some data and information on the web server through my iPhone application. I am not getting the way to post data to the web server from iPhone sdk.
It depends in what way you want to send data to the web server. If you want to just use the HTTP POST method, there are (at least) two options. You can use a synchronous or an asynchronous NSURLRequest. If you only want to post data and do not need to wait for a response from the server, I strongly recommend the asynchronous one, because it does not block the user interface. I.e. it runs "in the background" and the user can go on using (that is interacting with) your app. Asynchronous requests use delegation to tell the app that a request was sent, cancelled, completed, etc. You can also get the response via delegate methods if needed.
Here is an example for an asynchronous HTTP POST request:
// define your form fields here:
NSString *content = #"field1=42&field2=Hello";
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:#"http://www.example.com/form.php"]];
[urlRequest setHTTPMethod:#"POST"];
[urlRequest setHTTPBody:[content dataUsingEncoding:NSISOLatin1StringEncoding]];
// generates an autoreleased NSURLConnection
[NSURLConnection connectionWithRequest:request delegate:self];
Please refer to the NSURLConnection Class Reference for details on the delegate methods.
You can also send a synchronous request after generating the request:
[NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
If you pass a NSURLResponse ** as returning response, you will find the server's response in the object that pointer points to. Keep in mind that the UI will block while the synchronous request is processed.

Resources