Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8246455
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T22:30:38+00:00 2026-06-07T22:30:38+00:00

I want to send my JSON to a URL ( POST and GET ).

  • 0

I want to send my JSON to a URL (POST and GET).

NSMutableDictionary *JSONDict = [[NSMutableDictionary alloc] init];
[JSONDict setValue:"myValue" forKey:"myKey"];

NSData *JSONData = [NSJSONSerialization dataWithJSONObject:self options:kNilOptions error:nil];

My current request code isn’t working.

NSMutableURLRequest *requestData = [[NSMutableURLRequest alloc] init];

[requestData setURL:[NSURL URLWithString:@"http://fake.url/"];];

[requestData setHTTPMethod:@"POST"];
[requestData setValue:postLength forHTTPHeaderField:@"Content-Length"];
[requestData setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[requestData setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[requestData setHTTPBody:postData];

Using ASIHTTPRequest is not a liable answer.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T22:30:39+00:00Added an answer on June 7, 2026 at 10:30 pm

    Sending POST and GET requests in iOS is quite easy; and there’s no need for an additional framework.


    POST Request:

    We begin by creating our POST‘s body (ergo. what we’d like to send) as an NSString, and converting it to NSData.

    objective-c

    NSString *post = [NSString stringWithFormat:@"test=Message&this=isNotReal"];
    NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
    

    Next up, we read the postData‘s length, so we can pass it along in the request.

    NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
    

    Now that we have what we’d like to post, we can create an NSMutableURLRequest, and include our postData.

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
    [request setURL:[NSURL URLWithString:@"http://YourURL.com/FakeURL"]];
    [request setHTTPMethod:@"POST"];
    [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
    [request setHTTPBody:postData];
    

    swift

    let post = "test=Message&this=isNotReal"
    let postData = post.data(using: String.Encoding.ascii, allowLossyConversion: true)
    
    let postLength = String(postData!.count)
    
    var request = URLRequest(url: URL(string: "http://YourURL.com/FakeURL/PARAMETERS")!)
    request.httpMethod = "POST"
    request.addValue(postLength, forHTTPHeaderField: "Content-Length")
    request.httpBody = postData;
    

    And finally, we can send our request, and read the reply by creating a new NSURLSession:

    objective-c

    NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
    [[session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
        NSString *requestReply = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
        NSLog(@"Request reply: %@", requestReply);
    }] resume];
    

    swift

    let session = URLSession(configuration: .default)
    session.dataTask(with: request) {data, response, error in
        let requestReply = NSString(data: data!, encoding: String.Encoding.ascii.rawValue)
        print("Request reply: \(requestReply!)")
    }.resume()
    

    GET Request:

    With the GET request it’s basically the same thing, only without the HTTPBody and Content-Length.

    objective-c

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
    [request setURL:[NSURL URLWithString:@"http://YourURL.com/FakeURL/PARAMETERS"]];
    [request setHTTPMethod:@"GET"];
    
    NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
    [[session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
        NSString *requestReply = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
        NSLog(@"Request reply: %@", requestReply);
    }] resume];
    

    swift

    var request = URLRequest(url: URL(string: "http://YourURL.com/FakeURL/PARAMETERS")!)
    request.httpMethod = "GET"
    
    let session = URLSession(configuration: .default)
    session.dataTask(with: request) {data, response, error in
        let requestReply = NSString(data: data!, encoding: String.Encoding.ascii.rawValue)
        print("Request reply: \(requestReply!)")
    }.resume()
    

    On a side note, you can add Content-Type (and other data) by adding the following to our NSMutableURLRequest. This might be required by the server when requesting, e.g, a json.

    objective-c

    [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    [request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
    

    Response code can also be read using [(NSHTTPURLResponse*)response statusCode].

    swift

    request.addValue("application/json", forHTTPHeaderField: "Content-Type")
    request.addValue("application/json", forHTTPHeaderField: "Accept")
    

    Update: sendSynchronousRequest is deprecated from ios9 and osx-elcapitan (10.11) and out.

    NSURLResponse *requestResponse; NSData *requestHandler = [NSURLConnection sendSynchronousRequest:request returningResponse:&requestResponse error:nil]; NSString *requestReply = [[NSString alloc] initWithBytes:[requestHandler bytes] length:[requestHandler length] encoding:NSASCIIStringEncoding]; NSLog(@"requestReply: %@", requestReply);
    

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to send a JSON object from the client to an action on
I want to send a few variables and a string with the POST method
I want to send JSON request through HTTPConnection but i am getting error when
i want to send json to server using Spring 3.x, i use annotation @RequestBody,
I want to build an HTTP post that will send data and automatically bind
I want to send array from php to jquery using json. the array in
I want to send a json formatted string as a hidden field for a
I want to make a POST request to a URL like this: http://localhost/resource?auth_token=1234 And
I want send e-mail with some images in content. I think I must attached
I want send a email by Email Class in codeigniter with gmail, but i

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.