Note that there are some explanatory texts on larger screens.

plurals
  1. POSending a JSON via POST in NSURLRequest
    primarykey
    data
    text
    <p>I have a problem with sending a JSON to a Server with REST API. This is the code i use:</p> <pre><code>NSString *jsonPostBody = [NSString stringWithFormat:@"'json' = '{\"user\":{\"username\":" "\"%@\"" ",\"password\":" "\"%@\"" "}}'", [username stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding], [password stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; NSData *postData = [jsonPostBody dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES]; NSString *apiPathParams = [NSString stringWithFormat:@"%@", @"getUser" ]; NSURL *url = [NSURL URLWithString:[[apiPath retain] stringByAppendingString:apiPathParams]]; NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:180.0]; [request setHTTPMethod:@"POST"]; [request setHTTPBody:postData]; NSString* postDataLengthString = [[NSString alloc] initWithFormat:@"%d", [postData length]]; [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; [request setValue:postDataLengthString forHTTPHeaderField:@"Content-Length"]; [self internalRequest:request]; </code></pre> <p>This is what the API would look like, if it was a html form:</p> <pre><code>&lt;form method="post" action="someAPI-URL"&gt; &lt;input name="json" value="{"user":...}" /&gt; &lt;/form&gt; </code></pre> <p>This is what my POST Data looks like when i add it to the request:</p> <pre><code>json={"user":{"username":"someUser","password":"somePassword"}} </code></pre> <p>For a reason I don't know the POST Data does not arrive at the server. Have i done something wrong with the formatting of the dataString? How exactly must i format my dataString so that it matches the String a form as shown above would deliver to the server?</p> <p>Any help would be highly appreciated.</p> <p>P.S. I would rather not use ASIHttpRequest, since i took over the project from somebody else an every other request works fine, except this post-request. So changing this whole bulk to an other connection framework would be very time consuming.</p> <p>Here is the internalRequest Method's sourcecode</p> <pre><code>NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:req delegate:self]; NSMutableDictionary *di = [NSMutableDictionary dictionaryWithObject:[[NSMutableData alloc] init] forKey:@"receivedData"]; [di setObject:[NSString stringWithFormat:@"%i",identifier] forKey:@"identifier"]; if (delegate == nil) { delegate = self; } [di setObject:delegate forKey:@"delegate"]; CFDictionaryAddValue(connectionToInfoMapping, connection, di) </code></pre>
    singulars
    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.
 

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