2

I've got a problem with my objective c code. I have a API-key protected WCF API that I built that takes POST requests and writes them to a Java servlet with C#. Anyway, this works great when testing with Fiddler, not so good from objective C. When I try to run the POST from my objective C, it "acts" like the NSURLMutableRequest is looking for a GET, in that the response only returns some default code I have written in for the GET method. Does anybody know why this is, and, moreover, what I can do to fix it? Here is the code that I use (quite successfully) to make other POST requests in with objective C.

is the problem the fact that I specify the API key in the URL for the NSMutableRequest? That's the only thing I can figure.

Here is the code:

NSString* theMessage = [NSString stringWithFormat:@"<MyRequestObject xmlns='http://schemas.datacontract.org/2004/07/MyService'></MyRequestObject>"];

        NSMutableURLRequest *theRequest=[NSMutableURLRequest requestWithURL:[NSURL URLWithString:POST_API_URL] 
            cachePolicy:NSURLRequestUseProtocolCachePolicy
            timeoutInterval:240.0];

        [theRequest setHTTPMethod:@"POST"];
        [theRequest setValue:@"text/xml" forHTTPHeaderField:@"Content-Type"];
        [theRequest setHTTPBody:[theMessage dataUsingEncoding:NSUTF8StringEncoding]];

        NSString *msgLength = [NSString stringWithFormat:@"%d", [theMessage length]];
        [theRequest addValue: msgLength forHTTPHeaderField:@"Content-Length"];

        NSURLResponse* response;
        NSError *error;

        NSData* result = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:&response error:&error];
jdb1a1
  • 1,045
  • 1
  • 13
  • 32
  • Whats the response you getting back ? – lifemoveson Aug 13 '11 at 04:17
  • The response that gets returned is default text that I specify to return from a GET request, i.e., "Hello World!" – jdb1a1 Aug 13 '11 at 14:20
  • Can we see the WCF part a bit? Especially the interface/impl method signature with the [Attributes] – Oli Aug 13 '11 at 15:08
  • I don't have that code with me; it's at the office and I am working remotely. I am just using the WCF REST template downloaded from in VS2010 with the API key. – jdb1a1 Aug 13 '11 at 16:17

2 Answers2

1

I ended up using ASIHTTPRequest to run the POST request to the WCF REST service, and now everything seems to be running smoothly. This means that there's probably some sort of URL Encoding mechanism for the API key that's going on behind the scenes that was poorly documented for NSMutableURLRequest, who knows. The good thing is, I've got the issue fixed. Here is the code I used:

ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:POST_API_URL]];
        [request appendPostData:[[NSString stringWithFormat:@"<MyRequest xmlns='http://schemas.datacontract.org/2004/07/MyService'>all of my request params in here</MyRequest>"] dataUsingEncoding:NSUTF8StringEncoding]];

        [request setRequestMethod:@"POST"];
        [request addRequestHeader:@"Content-Type" value:@"text/xml"];

        [request startSynchronous];
jdb1a1
  • 1,045
  • 1
  • 13
  • 32
0

Did you try setting the Content-Length header? WCF/IIS might be ignoring the body if it doesn't have its length defined in as a header.

Oli
  • 1,031
  • 8
  • 20
  • Yeah, I tried that and it didn't change the results. Thanks for the idea, though. – jdb1a1 Aug 13 '11 at 14:25
  • RE:"Is the problem the fact that I specify the API key in the URL for the NSMutableRequest?" How is the server expecting the API key to come in? – Oli Aug 13 '11 at 15:07
  • As part of the URL, i.e., http://www.myserver.com/myservice?apikey=12345678-1234-1234-1234-123456789876 – jdb1a1 Aug 13 '11 at 16:02
  • I'm using WCF routes to take in the URL, which then goes and checks for the API key against a server side store for a match. If it find the key, it returns the data. – jdb1a1 Aug 13 '11 at 16:03
  • I have done further research, and have seen other examples where the API key is used in the URL for the NSMutableURLRequest, so I don't think that's the problem. At this point, my best guess is that something is going on in the WCF where it doesn't like how I submit the request and it defaults to the GET instead of the POST. – jdb1a1 Aug 13 '11 at 16:05
  • I guess the key question is this: why would a WCF web service POST request work exactly as you expect it to when testing with fiddler,but not in your client-side objective C code? – jdb1a1 Aug 13 '11 at 16:55