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.
Related
I'm using mapbox-gl-native SDK. It can be used with custom servers to provide map tilesets. I'm working with a server which needs an API key in the HTTP request header. I want to edit the header for every request and add the API key.
can anyone help me with this?
I'm using mapbox-gl-native iOS v5.0 (latest untill now)
there is a file named http_file_source.mm in platform/darwin/src/ which is responsible for making HTTP requests. there is a method in this file:
HTTPFileSource::request(const Resource& resource, Callback callback) {
in this mehtod you can see this line:
NSMutableURLRequest* req = [NSMutableURLRequest requestWithURL:url];
this line makes a http request you can add your own header to it, like so: (value for header field)
[req addValue:(NSString *)value forHTTPHeaderField:(NSString *)field]
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.
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"];
?
I am having some trouble using HTTP token authentication with a NSMutableURLRequest. I have done some research and have used the following question: ios managing authentication tokens from NSURLRequest / HTTP request to learn how to set the token in the HTTP header. However the token I need to set isn't just a simple header field with a value. I need to set an http header field (or multiple) to look like this: Authorization: Token token="tokenHere", nonce="def". The following code snippets are examples of things I have tried that have failed:
//failed to authenticate with server
[request setValue:#"tokenHere" forHTTPHeaderField:#"token"];
[request setValue:#"def" forHTTPHeaderField:#"nonce"];
//failed as well
NSDictionary *authToken = [NSDictionary dictionaryWithObjectsAndKeys:
#"tokenHere", #"token",
#"def", #"nonce",
nil];
[request setValue:[NSString stringWithFormat:#"Token %#", authToken] forHTTPHeaderField:#"Authorization"];
//failed again
[request setValue:#"Authorization: Token token='tokenHere', nonce='def'" forHTTPHeaderField:#"Authorization"];
The back end server is programmed in ruby, and is expecting a single header that looks just like the example above. I need some help crafting the value so that it works with the back end.
It's just a guess, since the right answer depends entirely on what the server expects; but, using other auth schemes as a model, the header would look something like this:
NSString *authHeader = [NSString stringWithFormat:#"token=\"%#\", nonce=\"%#\"", #"tokenHere", #"def"];
[myMutableRequest setValue:authHeader forHTTPHeaderField:#"Authorization"];
In other words, the name of the header field is "Authorization" and it's value is comma separated name=value pairs. The only part of your spec that doesn't make sense is the extra mention of 'token', as in
... Token token=...
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.