php - HttpURLConnection setRequestMethod doesn't work properly -


i'm trying consume api rest made laravel 4, when try create new resource (store, post method) response of index function (get method).

i don't know whats happening code:

public static string sendinfo(hashmap<string, string> headers) {     url url;     httpurlconnection urlconnection = null;     stringbuilder stringbuilder = new stringbuilder();     try {         url = new url(headers.get("url"));          urlconnection = (httpurlconnection) url.openconnection();         urlconnection.setdooutput(true);         urlconnection.setrequestmethod(headers.get("method"));          // loop hashmap , apply headers         (hashmap.entry<string, string> entry : headers.entryset()) {             // need headers real information. url , method headers setrequestmethod , object url.             if (!entry.getkey().equals("url") && !entry.getkey().equals("method")) {                 urlconnection.addrequestproperty(entry.getkey(), entry.getvalue());             }         }         if (urlconnection.getresponsecode() == 200) {             bufferedreader reader = new bufferedreader(new inputstreamreader(urlconnection.getinputstream()));             string line;             while ((line = reader.readline()) != null) {                 stringbuilder.append(line);             }             reader.close();         } else {             log.d("sendinfo", "error " + headers.get("url") + "  state: " + urlconnection.getresponsecode());         }     } catch (exception e) {         e.printstacktrace();     } {         try {             urlconnection.disconnect();          } catch (exception e) {             e.printstacktrace();             log.d("sendinfo", "error disconnect: " + e.getlocalizedmessage());         }     }     log.d("response", "server response: "+stringbuilder.tostring());      return stringbuilder.tostring(); } 

i call method follow:

hashmap<string, string> headers = new hashmap<string, string>(); headers.put("url", "http://foo.com/api/person/"); headers.put("method", "post"); headers.put("name", "jason"); headers.put("lastname", "harrison"); sendinfo(headers); 

​but intead of entry in store function of laravel resource, i'm getting response of index​ function.

​i put code in index check http method:

​dd("index: ".$_server['request_method']);​

​and returns "get", wrong in code​​. works put , get http methods, fail post

i confirm empty body not problem, being try that.

may try code, please?

i'm desperate. ask you, test code , guide me in right direction, please...

you should take @ retrofit. lib make api call more cleaner , avoid problems encounter building request on top of httpurlconnection.

edit: if want request take @ this post. problem similar yours. seems need send data in request body. btw sending parameters via headers isn't way go. think there might lenght restriction plus might encounter encoding issues.

headers should used http related information or tokens. should use body send data. data of time sent using json/xml or httpurlencoded params.

edit 2:

i tried exact same code wamp server , $_server['request_method'] returned post. maybe issue commes server. try testing server api post man chrome plugin see side error comes from.


Comments

Popular posts from this blog

How has firefox/gecko HTML+CSS rendering changed in version 38? -

javascript - Complex json ng-repeat -

jquery - Cloning of rows and columns from the old table into the new with colSpan and rowSpan -