php - Is this a properly formatted POST request? -
is below code formatted post request? seems entire contents being passed via uri , not via post. have primitive understanding of php , url requests in general server-side code has been included below well.
i noticed issue while attempting upload base64 encoded images (as demonstrated below) server. upon submitting request, server return error stating uri exceeds maximum length, leads me believe isn't proper approach submitting post request. entire process works if image 10x10px, isn't going trick , main priority here understanding correct approach.
- what proper approach submitting post request, similar below example?
- how can post data received server , json need slashed stripped if submitted via post?
swift
if let imagedata = uiimagepngrepresentation(self.image!) { let encodedimage = imagedata.base64encodedstringwithoptions(nsdatabase64encodingoptions(rawvalue: 0)) let dict = ["barcode":self.barcode, "imagedata":encodedimage] let json = jsonstringify(dict, prettyprinted: false) let jsonencoded = cfurlcreatestringbyaddingpercentescapes( nil, json, nil, "!*'();:@&=+$,/?%#[]", cfstringbuiltinencodings.utf8.rawvalue) let server:string = globalvars.serveraddress let phpfile:string = "/php/inventory/updateitemimage.php" let baseurl = nsurl(string: "\(server)\(phpfile)") let url = nsurl(string: "?json=\(jsonencoded)", relativetourl: baseurl) let cachepolicy = nsurlrequestcachepolicy.reloadignoringlocalcachedata let request = nsmutableurlrequest(url: url!, cachepolicy: cachepolicy, timeoutinterval: 10.0) request.httpmethod = "post" let datastring = "" let requestbodydata = (datastring nsstring).datausingencoding(nsutf8stringencoding) request.httpbody = requestbodydata var response: nsurlresponse? = nil { let reply = try nsurlconnection.sendsynchronousrequest(request, returningresponse:&response) return true } catch { return false } }
php
<?php // setup db connection etc.. //.. //define passed variables $json = $_get['json']; $json = stripslashes($json); $fieldarray = json_decode($json); $barcode = $fieldarray->{"barcode"}; $imagedata = $fieldarray->{"imagedata"}; // etc.... ?>
Comments
Post a Comment