java - RestEasy : How to send an easy POST request with parameters and api key -
i'm beginner , i'm little bit lost resteasy
i'd send post request url similar : http://myurl.com/options?value=3name=picture
string myvalue = "3"; string myname = "picture"; string key = "topsecret";
i'm not sure what's coming. i've seen several tutorial classes (not clear me) , way similar this
final multivaluedmap<string, object> queryparams = new multivaluedmapimpl<>(); queryparams.add("value", myvalue); queryparams.add("name", mypicture); final resteasyclient client = new resteasyclientbuilder().build(); final resteasywebtarget target = client.target(url).queryparams(queryparams);; final builder builder = target.request();
when write have loads of warning. right way ? api key ?
first of all, must check documentation of api want consume regarding how api key must sent server. not apis follow same approach.
for example purposes, let's assume api key must sent in x-api-key
header. it's non standard , i've made demonstrate how use client api.
so can have following:
// create client client client = clientbuilder.newclient(); // define target webtarget target = client.target("http://myurl.com/options") .queryparam("value", "3") .queryparam("name", "picture"); // perform request target response response = target.request().header("x-api-key", "topsecret") .post(entity.text("")); // process response // part // close response response.close(); // close client client.close();
the above code uses jax-rs api, implemented resteasy. you'd better use client
instead of resteasyclient
whenever possible ensure portability other implementations.
the above code assumes want send empty text in request payload. modify accordingly.
response
instances contain un-consumed entity input stream should closed. typical scenarios response headers , status code processed, ignoring response entity.
going beyond scope of question, bear in mind client
instances heavy-weight objects manage underlying client-side communication infrastructure. hence initialization disposal of client
instance may rather expensive operation.
the documentation advises create small number of client
instances , reuse them when possible. states client
instances must properly closed before being disposed avoid leaking resources.
Comments
Post a Comment