Note that there are some explanatory texts on larger screens.

plurals
  1. POBad formatting: Add JSON data to existing URL
    primarykey
    data
    text
    <p>I need to send request like this:</p> <pre><code>"http://api.site.com/login?q={"meta":{"api_key":"cb2f734a14ee3527b3"},"request":{"id":"username@host.name","password":"passw0rd"}}` </code></pre> <p>...the response to which should look like <code>{"id":399205,"token":"d43f8b2fe37aa19ac7057701"}</code> To do so, I have tried the following code:</p> <pre><code>self.responseData = [NSMutableData data]; NSDictionary *apiKeyDict = [NSDictionary dictionaryWithObject:@"cb2f734a14ee3527b3" forKey:@"api_key"]; NSDictionary *idPasswordDict = [NSDictionary dictionaryWithObjectsAndKeys:@"tc-d@gmail.com",@"id", @"abc",@"password", nil]; NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:apiKeyDict, @"meta", idPasswordDict, @"request", nil]; NSURL *url = [NSURL URLWithString:@"http://api.site.com/login?="]; NSError *error = nil; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0]; NSData *requestData = [NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:&amp;error]; [request setHTTPMethod:@"POST"]; [request setValue:@"application/json" forHTTPHeaderField:@"Accept"]; [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; [request setValue:[NSString stringWithFormat:@"%d", [requestData length]] forHTTPHeaderField:@"Content-Length"]; [request setHTTPBody: requestData]; NSURLConnection *connection = [[NSURLConnection alloc]initWithRequest:request delegate:self]; if (connection) { self.recievedData = [NSMutableData data]; } </code></pre> <p>Later on, I get in the following:</p> <pre><code>- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { [self.recievedData appendData:data]; NSLog(@"%@",[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding]); } </code></pre> <p>The response data comes back as "<code>&lt;h1&gt;CHttpException&lt;/h1&gt;&lt;p&gt;wrong sign&lt;/p&gt;</code>" As I understand, the way of adding json data to the current url is not appropriate in this case. Does anyone have advice on how I can fix this?</p> <p>resolve my problem. Convert posted json to appended string to base url-string. that's how I did it:</p> <pre><code>NSData *requestData = [NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:&amp;error]; NSMutableString *dictString = [[NSMutableString alloc]initWithData:requestData encoding:NSUTF8StringEncoding]; [dictString insertString:@"http://api.site.com/login?q=" atIndex:0]; //don't know why but dictString contains a lot of @"\n" and spaces to present it nice-formated. [dictString replaceOccurrencesOfString:@"\n" withString:@"" options:nil range:NSMakeRange(0, dictString.length)]; [dictString replaceOccurrencesOfString:@" " withString:@"" options:nil range:NSMakeRange(0, dictString.length)]; NSURL *url = [NSURL URLWithString:[dictString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; </code></pre> <p>if it helps someone will appreciate tick near this issue=)</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload