2

I've been searching for hours to a solution to this problem, and I can find nothing that solves my issue. I receive Invalid JSON Primitive whenever I attempt to send a URL with a string value (NSString) in it to my server via an HTTP Get.

My code:

Client Side Obj-C

- (void) createRequest: (NSString*)urlFormatted {

    NSURL *url = [NSURL URLWithString: urlFormatted];
    NSMutableURLRequest *request =[NSMutableURLRequest requestWithURL: url];

    [request setHTTPMethod:@"GET"];
    [request setValue:@"application/json; charset=UTF-8" forHTTPHeaderField:@"Content-Type"];
    _webData = [[NSMutableData data] retain];
    _connection = [[NSURLConnection alloc] initWithRequest:request  delegate:self];
}


-(void) authenticate:(NSString*) userName password:(NSString*)password
{
    NSString*url = @"http://service.example.com/service.asmx/Test?test1=hi";
    [self createRequest:url];
    [url release];
}

Server Side C#

This does not work. Whenever I have a string as a parameter, I receive the message,

Invalid JSON Primitive e.g.

[ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
public string Test(string test1)
{
   return "Ok.";  // For testing purposes, just return a string.
}

This however, works fine. (Obviously if I change my test1 parm to an integer)

[ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
public string Test(int test1)
{
   return "Ok.";  // For testing purposes, just return a string.
}

Everything I have read points me to ensure that I have application/json set - which it is. Whenever I don't use strings in my URL, everything is fine. I don't understand why this doesn't work. It doesn't looking like an encoding issue to me. Any ideas?

George Johnston
  • 31,652
  • 27
  • 127
  • 172
  • Have you checked the request using a tool like Fiddler to see what if any differences there are in the querystring data between the string and the integer? – DaveB Apr 27 '11 at 22:08
  • Similar issue - http://stackoverflow.com/questions/7830961/invalid-json-primitive-id – Vas May 08 '13 at 16:28

1 Answers1

4

You need to enclose the string within double-quotes i.e the URL should be

http://service.example.com/service.asmx/Test?test1="hi"
amit_g
  • 30,880
  • 8
  • 61
  • 118