Hi there,
I'm working with a private API and the guy in charge of the API asked me to change the client name I send to the server when making API calls.
I'm using NSMutableURLRequest and for now, I send something like :
< my app name > iOS App version < version of my app >
But that's automatically set and I don't know how to change it.
Does any of you guys have an idea ?
Thanks in advance !
Something like this:
[theRequest addValue:#"MyApplication v1.X.X" forHTTPHeaderField:#"MyAppName"];
You might also consider using the "User-Agent" header:
[theRequest addValue:#"MyApplication v1.X.X" forHTTPHeaderField:#"User-Agent"];
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 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"];
?
So i am trying to make an http request using ASIHTTPRequest class , to send a json file to my server. I was given an android code which is functional and was asked to convert it to iOS code.
I cant understand how to add headers on my request.The android code looks something like this :
private static final String CONTENT_TYPE = "Content-Type";
private static final String JSON_CONTENT_TYPE = "application/json; charset=utf-8";
and then :
request.addHeader(CONTENT_TYPE, JSON_CONTENT_TYPE);
On iOS i figured out that the code is something like this :
[request addRequestHeader:#"Content-Type" value:#"Content-Type"];
[request addRequestHeader:#"Json-Content-Type" value:#"application/json; charset=utf-8"];
BUT i got a bit confused , cause on android the request seems to be paired and on iOS the requestHeaders are individually stated. Is this a problem? Should they be paired too on iOS. Am i missing something here? Thank you for reading my post :D
What you want is
[request addRequestHeader:#"Content-Type" value:#"application/json; charset=utf-8"];
in the android example, they are just declaring two variables for the header values, they could had simply done something like
request.addHeader("Content-Type", "application/json; charset=utf-8");
which would have had the same effect, so you just need the one line i mentioned above.
also: ASIHTTPREQUEST does not get developed any more (see headline here), so consider using another library for something like this, like AFNetworking
We have a .NET WCF Service talking to an iPhone app. I'm using wsdl2objc to generate the objc code required for all the SOAP magic. SoapUI is able to add this service's wsdl and send requests correctly.
But the request generated by wsdl2objc complains about this:
<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope">
<s:Body>
<s:Fault>
<s:Code>
<s:Value>s:Sender</s:Value>
<s:Subcode>
<s:Value xmlns:a="http://schemas.microsoft.com/ws/2005/05/addressing/none">a:ActionNotSupported</s:Value>
</s:Subcode>
</s:Code>
<s:Reason>
<s:Text xml:lang="en-US">The message with Action '' cannot be processed at the receiver, due to a ContractFilter mismatch at the EndpointDispatcher. This may be because of either a contract mismatch (mismatched Actions between sender and receiver) or a binding/security mismatch between the sender and the receiver. Check that sender and receiver have the same contract and the same binding (including security requirements, e.g. Message, Transport, None).
</s:Text>
</s:Reason>
</s:Fault>
</s:Body>
</s:Envelope>
However, If I use the same s:Envelope with SoapUI, the request works correctly. The only difference between the two requests as far as I can see is the header section where wsdl2objc generates this:
SOAPAction:http://xx/AuthenticateDevice
but soapUI generates:
Content-Type: application/soap+xml;charset=UTF-8;action="http://xx/AuthenticateDevice"
I tried to change the code that wsdl2objc generated and replaced this:
[request setValue:soapAction forHTTPHeaderField:#"SOAPAction"];
NSString *contentType = [NSString stringWithFormat:#"application/soap+xml; charset=utf-8;"];
[request setValue:contentType forHTTPHeaderField:#"Content-Type"];
with this:
[request setValue:soapAction forHTTPHeaderField:#"action"];
NSString *contentType = [NSString stringWithFormat:#"application/soap+xml; charset=utf-8;"];
[request setValue:contentType forHTTPHeaderField:#"Content-Type"];
But, I just get a 400 error now. Any help is much appreciated!
Thanks,
Teja.
I had a similar issue that was fixed by changing the envelope schema URL in the wsdl2objc output
I changed this
xmlNsPtr soapEnvelopeNs = xmlNewNs(root, (const xmlChar*)"http://www.w3.org/2003/05/soap-envelope", (const xmlChar*)"soap");
to This:
xmlNsPtr soapEnvelopeNs = xmlNewNs(root, (const xmlChar*)"http://schemas.xmlsoap.org/soap/envelope/", (const xmlChar*)"soap");
and it seemed to fix my WCF web-service calls from iOS.
What I think this means is that my version of wsdl2objc is outputting a SOAP 1.1 envelope when the WCF server seems to want SOAP 1.2
Has anyone else noticed this? Is there another way to configure this rather than change after generating the code?
It appears that I was missing begin quote and end quote tags around the action value which was causing this. Here's the code that worked for me, but I'm not happy with setting the action as a part of the http content-type header. So if someone can come up with a better solution, I'd be happy to give credit:
NSString *contentType = [NSString stringWithFormat:#"application/soap+xml; charset=utf-8; action=\"%#\"", soapAction];
[request setValue:contentType forHTTPHeaderField:#"Content-Type"];
In the WCF services I've worked with I've just passed the value "text/xml" in the Content-Type header and then passed the correct soap action value in a separate SOAPAction header (similiar to the way wsdl2objc was doing it).
Here are a couple things to check:
Is your WCF service using basicHttpBinding - or something else. (I seem to remember reading that bindings other than basicHttpBinding don't work well (or at all) with iOS - but I'm not certain)
What is the correct SOAP action value for this service? In the services I work with it's built using <ServiceNamespace>\<WCFInterfaceName>\<MethodName>
I'm having the strangest issue right now with google app engine. I'm sending a POST request from iOS and google app engine instead invokes the GET handler.
I've sandboxed this one situation for testing and can't get it figured out. I have an iOS app that just sends a request. And I've commented out everything on GAE except for the service. The service only logs a parameter and returns.
The iOS app I've tried using two different ways of sending the request. Neither works.
iOS Code:
/*
NSURL * url = [NSURL URLWithString:#"http://beermonster-gngrwzrd.appspot.com/TestParameter"];
ASIFormDataRequest * _fdrequest = [[ASIFormDataRequest alloc] initWithURL:url];
[_fdrequest setPostValue:#"hello" forKey:#"testkey"];
[_fdrequest startAsynchronous];
*/
NSURL * __url = [NSURL URLWithString:#"http://beermonster-gngrwzrd.appspot.com/TestParameter"];
NSMutableURLRequest * __request = [NSMutableURLRequest requestWithURL:__url];
[__request setHTTPMethod:#"POST"];
NSString * post = [NSString stringWithFormat:#"testkey=hello"];
[__request setHTTPBody:[post dataUsingEncoding:NSUTF8StringEncoding]];
[NSURLConnection sendSynchronousRequest:__request returningResponse:nil error:nil];
My App engine handler:
class TestParameter(webapp.RequestHandler):
def post(self):
logging.debug(self.request.get("testkey"))
self.response.out.write(self.request.get("testkey"))
print self.request.get("testkey")
def get(self):
logging.debug("get")
logging.debug(self.request.get("testkey"))
self.response.out.write(self.request.get("testkey"))
The output in the GAE logs shows the "get" code path which isn't correct.
Any ideas why POST requests would come into GAE as a GET? Is there some configuration in GAE that I missed?
Thanks!
Check the entry in app.yaml for the script that handles "/TestParameter". Does it specify "secure: always"? If it does and you make a non-secure connection you will get a 302 redirecting to the secure version.
To fix this either make your post over HTTPS or remove "secure: always" from the entry in app.yaml.
From what I can tell if you want to send POST requests to GAE. Make sure you do it on https. If you make the request on a non-https attempt, it sends back a 302 redirect to the https version of the request. But if whatever you're using to send the request doesn't correctly handle 302's it might resend the request incorrectly.