NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:tempURL]] autorelease];
[request setValue:encryptedValue forHTTPHeaderField:#"EncString"];
I could spot all other headers in the request but this specific header is missing, can any one let me know what could be the reasons.
I have tried the following code:
NSString *tempURL = #"https://stackoverflow.com/questions/50701101/unable-to-add-a-header-to-nsmutablerequest";
NSString *encryptedValue = #"whatever_that_has_no_special_characters_or_values_that_would_make_you_think_it_is_relevant_to_reproduce_this_issue";
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:tempURL]];
[request setValue:encryptedValue forHTTPHeaderField:#"EncString"];
NSLog(#"%#", request.allHTTPHeaderFields);
The output is:
{
EncString = "whatever_that_has_no_special_characters_or_values_that_would_make_you_think_it_is_relevant_to_reproduce_this_issue";
}
which looks good. There is no reason to suspect the code you provided is responsible for the issue. Maybe you should check one of the following:
Is encryptedValue truly a valid object to be placed into header (is string, not too long, no very special characters)
Is the object you are printing really this specific request (printing NSLog(#"%p", request); returns same result)
In no place in this object a herder is being removed (if nothing else subclass NSMutableURLRequest and override relevant methods to track what is going on)
If none of these work create a post with additional details on what you have done, where are you checking these headers and "could spot all other headers".
Related
I know I can use the below code to change the default agent:
NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:#"Your user agent", #"UserAgent", nil];
[[NSUserDefaults standardUserDefaults] registerDefaults:dictionary];
but I want to request a different User-Agent for different requests. Examples: request 'www.google.com', use agent=‘google Agent’, request 'www.github.com', use agent='github Agent'.
I have tried the below way to set the 'User_Agent' in each request, but it doesn't seem to work.
- (void)viewDidLoad {
NSString *urlAddress = #"http://www.amazon.com";
//Create a URL object.
NSURL *url = [NSURL URLWithString:urlAddress];
//URL Requst Object
NSMutableURLRequest *requestObj = [NSMutableURLRequest requestWithURL:url];
[requestObj setValue:#"Foobar/1.0" forHTTPHeaderField:#"User_Agent"];
// But the value is not "Foobar/1.0"
NSString* secretAgent = [webView stringByEvaluatingJavaScriptFromString:#"navigator.userAgent"];
NSLog(#"agent - %#", secretAgent);
//Load the request in the UIWebView.
[webView loadRequest:requestObj];
}
One more question:
It seems a change to the default agent only work change in 'AppDelegate' or 'initialize'. Am I right? Because I try to change in 'viewDidLoad' but it doesn't seem to work.
I observed that if you load any url in webView with a user-agent set
to it. After some time you load the different url & different user
agent but used the same allocated WebView instane.The web view will
load the new url but can not set new user-agent. It just set old one
that you have set before loading first time.
You can not change untill you initiate new instanse of UIWebview(or
we can say that user agent can be set once per session). So I coame to result that user-agent can be set only once.
What I did for this problem is, I allocated new instance when I want set new user-agent.
You don't need to set it using JavaScript.
You have misspelled User-Agent. Use this minimal code taken from here
NSString* userAgent = #"My Cool User Agent";
NSURL* url = [NSURL URLWithString:#"http://whatsmyuseragent.com/"];
NSMutableURLRequest* request = [[NSMutableURLRequest alloc] initWithURL:url];
[request setValue:userAgent forHTTPHeaderField:#"User-Agent"];
Does Foundation have NSString constants for the supported HTTPMethod values (eg for GET, POST, etc)?
Searched the NSURLRequest docs, but couldn't find anything.
To my knowledge, NSURLRequest doesn't have any constants for HTTP method types. You could, however, define your own and use them elsewhere. Something like this:
//Somewhere
#define setPOST(request) [request setHTTPMethod:#"GET"]
//...later in code
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
setPOST(request);
I would also highly recommend using AFNetworking. With that library, you can easily do things like:
[connectionMgr POST:url parameters:submitDict success:nil failure:nil]; //Obviously bare-bones, you'd need to fill in the parameters and blocks
I need to send a image to server with Url the url looks as below
http://somewebsite.com:8080/app/api/fappname/newUser/1/Delhi481/cct/12 dk/11588/kmkr/72579/Los/TM/8:00/13:00/28.6100/77.2300/foto1.jpg/null
they asked me to send image in place of foto1.jpg, the code that now i'm using is as below i am sending null inplace of image i'm getting response. can any one send me how to send the image in this url.
NSString *str = [NSString stringWithFormat:#"http://someurl.com:8080/app/api/kkknnmm/newUser/1/%#/%#/%#/%#/%#/%#/%#/TM/%#/%#/%#/%#/%#/%#/%#",[inSchoolName stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],#"kktt",[inAddressStreet stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],inAddressNo,inAddressState,inZipCode,[inNeighbourhood stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],[arrivalHR stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],[endingHr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],inlat,inlong,inImg1,inImg2,inImg3];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:str]];
NSLog(#"Login Url:%#",str);
connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
can any one please help me.
Well your Api Is expecting binary data, I would suggest you to go through these links.
I hope they Will Help you.
Have A look at this And this
I have succeeded in making a post using a HTTP Client by setting the content type as application/json and this json code:
{
"order": {
"name": "Tia Carter",
"location": "Corams",
"phone_number": "707",
"food": "Bobcat Burger"
}
}
The code works perfect and the order is registered in the database. I am trying to work this into my iOS app but keep getting syntax errors regarding the colons in the json. This is the objective-c code:
NSURL *nsURL = [[NSURL alloc] initWithString:#"http://0.0.0.0:3000/orders.json"];
NSMutableURLRequest *nsMutableURLRequest = [[NSMutableURLRequest alloc] initWithURL:nsURL];
// Set the request's content type to application/x-www-form-urlencoded
[nsMutableURLRequest setValue:#"application/json" forHTTPHeaderField:#"Content-Type"];
// Set HTTP method to POST
[nsMutableURLRequest setHTTPMethod:#"POST"];
// Set up the parameters to send.
NSString *paramDataString = [NSString stringWithFormat:#
"{ "order": {"%#:" ="%#","%#:"="%#","%#:"="%#","%#:"="%#"}}", #"name", _name.text, #"location", _location.text, #"phone_number", _phoneNumber.text, #"food", _order.text];
// Encode the parameters to default for NSMutableURLRequest.
NSData *paramData = [paramDataString dataUsingEncoding:NSUTF8StringEncoding];
// Set the NSMutableURLRequest body data.
[nsMutableURLRequest setHTTPBody: paramData];
// Create NSURLConnection and start the request.
NSURLConnection *nsUrlConnection=[[NSURLConnection alloc]initWithRequest:nsMutableURLRequest delegate:self];
I'd appreciate any ideas or guidance. Thanks.
I believe you have two problems:
You didn't escape quotas (put \ before all of them)
You don't need to put text "name", "location" and etc in parameters (it's not a problem per se, just a style thing)
Also, I would recommend to work with NSDictionary and convert it to JSON when you need to (it will save you a lot of nerves for unescaped quotas, missing bracket and so on).
Look this question how to convert NSDictionary to JSON:
Generate JSON string from NSDictionary in iOS
I'm trying to get into a webAPI my classmate created with my iphone APP I've created. Right now, it connects, but my app just crashes because it tells me that I need authorization. So if for example, the username is abc and password abc123, How would I implement the HTTP method specifically to get passed authorization??
NSString *geturl = #"http://192.168.1.96:50364/api/transaction" ;
setValue:(NSString*)" " forHTTPHeaderField:(NSString*)username;
getjson *mds=[[getjson alloc] init];
SEL selector = #selector(addData:);
[mds setDirectionsQuery:geturl
withSelector:selector
withDelegate:self];
Edit: if the above problem is too confusing, what is the basic syntax/code for setting up the value and the key for username and password? I am sure
setValue:(NSString*)"abc " forHTTPHeaderField:(NSString*)username;
setValue:(NSString*)"abc123 " forHTTPHeaderField:(NSString*)password;
is not correct. compiler is not letting me run the program.
Your strings are missing #
Try setValue:(NSString*)#"abc " forHTTPHeaderField:(NSString*)username; although if the code you pasted is complete, it doesn't say what your setting values to.
I personally use NSMutableRequest and create the request and authorisation like this. Note that the auth gets Base64 encoded:
NSMutableURLRequest *urlRequest = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:_urlString]];
NSString *basicAuthCredentials = [NSString stringWithFormat:#"%#:%#", username, password;
NSString *authValue = [NSString stringWithFormat:#"Basic %#", AFBase64EncodedStringFromString(basicAuthCredentials)];
[urlRequest setValue:authValue forHTTPHeaderField:#"Authorization"];