objective c - How to send POST and GET request? -
i want send json
url (post
, 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
not liable answer.
sending post
, get
requests in ios quite easy; , there's no need additional framework.
post
request:
we begin creating our post
's body
(ergo. we'd send) nsstring
, , converting nsdata
.
nsstring *post = [nsstring stringwithformat:@"test=message&this=isnotreal"]; nsdata *postdata = [post datausingencoding:nsasciistringencoding allowlossyconversion:yes];
next up, read postdata
's length
, can pass along in request.
nsstring *postlength = [nsstring stringwithformat:@"%d", [postdata length]];
now have we'd post, can create nsmutableurlrequest
, , 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];
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, can send our request, , read reply creating new nsurlsession
:
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];
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 get
request it's same thing, without httpbody
, content-length
.
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];
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 side note, can add content-type
(and other data) adding following our nsmutableurlrequest
. might required server when requesting, e.g, json.
[request setvalue:@"application/json" forhttpheaderfield:@"content-type"]; [request setvalue:@"application/json" forhttpheaderfield:@"accept"];
response code can read using [(nshttpurlresponse*)response statuscode]
.
request.addvalue("application/json", forhttpheaderfield: "content-type") request.addvalue("application/json", forhttpheaderfield: "accept")
update: sendsynchronousrequest
deprecated ios9 , osx-elcapitan (10.11) , 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);
Comments
Post a Comment