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.
By using RestKit you can make a simple POST request (see this GitHub page for more details).
Import RestKit in your header file.
Then you can start by creating a new
RKRequest
.Then specify what kind of request you want to make (in this case, a
POST
request).And then set your request as a JSON in
additionalHTTPHeaders
.Finally, you can send the request.
Additionally you can
NSLog
your request to see the outcome.Sources: RestKit.org and Me.
Sending
POST
andGET
requests in iOS is quite easy; and there's no need for an additional framework.POST
Request:We begin by creating our
POST
'sbody
(ergo. what we'd like to send) as anNSString
, and converting it toNSData
.objective-c
Next up, we read the
postData
'slength
, so we can pass it along in the request.Now that we have what we'd like to post, we can create an
NSMutableURLRequest
, and include ourpostData
.swift
And finally, we can send our request, and read the reply by creating a new
NSURLSession
:objective-c
swift
GET
Request:With the
GET
request it's basically the same thing, only without theHTTPBody
andContent-Length
.objective-c
swift
On a side note, you can add
Content-Type
(and other data) by adding the following to ourNSMutableURLRequest
. This might be required by the server when requesting, e.g, a json.objective-c
Response code can also be read using
[(NSHTTPURLResponse*)response statusCode]
.swift
Update:
sendSynchronousRequest
is deprecated from ios9 and osx-elcapitan (10.11) and out.Get Request :
JSON Response: {"status":"success","name":"project1","address":"badkal mor metro station","state":"haryana","city":"faridabad","start_time":"1480586013","current_stage":"Tender Acceptance","manager":"Not Available","completion_time":"1480464000"}
you can get keys value like name,start_time etc. and show on label
view control.h
view.m
tablecell.h