ios - Differences between Using NSJSONSerialization.dataWithJSONObject and dataUsingEncoding to make up the HTTP body in Swift -


i have seen 2 kinds of methods make http body.
first 1 is:

let url = nsurl(string: "http://example.com")   let request = nsmutableurlrequest(url: url!)   request.httpmethod = "post"   let poststring = "id=13&name=jack"   request.httpbody = poststring.datausingencoding(nsutf8stringencoding)   

second 1 is:

let url = nsurl(string: "http://example.com")   let request = nsmutableurlrequest(url: url!)   request.httpmethod = "post"   let params = ["id":"13", "name":"jack"] dictionary<string, string> var err: nserror? request.httpbody = nsjsonserialization.datawithjsonobject(params, options: nil, error: &err) 

when directly print out request.httpbody data different. wondering there differences between these 2 methods in terms of implementation of server side? assuming i'am using php.

there're 2 format data.

in code using poststring.datausingencoding send data in urlencoded format. in client must set request's content-type header "application/x-www-form-urlencoded" or "application/x-www-form-urlencoded charset=utf-8"

in code using nsjsonserialization.datawithjsonobject send data in json format. in client must set request's content-type header field "application/json"

i'm ios dev don't know format's effect server side php. answer question must find difference between application/x-www-form-urlencoded , application/json format in server side


Comments

Popular posts from this blog

OpenCV OpenCL: Convert Mat to Bitmap in JNI Layer for Android -

android - org.xmlpull.v1.XmlPullParserException: expected: START_TAG {http://schemas.xmlsoap.org/soap/envelope/}Envelope -

python - How to remove the Xframe Options header in django? -