java-curl icon indicating copy to clipboard operation
java-curl copied to clipboard

How to get response and it header ??

Open k870611 opened this issue 6 years ago • 7 comments

It is first to use this tool, and I don't know how to get response from it.

For example, I curl a url like "https://www.github.com" then How can I see the response and it header from it ??

k870611 avatar Nov 15 '19 01:11 k870611

CUrl curl = new CUrl('https://www.github.com'); String resp = curl.exec(CUrl.UTF8, null); List<String[]> headers = curl.getResponseHeaders()[0];

rockswang avatar Nov 15 '19 01:11 rockswang

Thanks, It works, but I still have some question

  1. if url is can't visit it will raise IOException, how to ignore it ??
  2. can see response in key-value ??
  3. it RESTful work now (like --request POST, DELETE), if not, how to use it like RESTful api ??

k870611 avatar Nov 15 '19 03:11 k870611

  1. you can write your own Resolver to avoid throwing a fatal Exception
  2. you need other library to parse json data, e.g. jackson, gson etc.
  3. they should be working, if not, please report another issue

rockswang avatar Nov 15 '19 06:11 rockswang

Can you teach how to use get, post, delete by this jar ?? because doc says --request is not support on this jar.

k870611 avatar Nov 15 '19 07:11 k870611

'--request' or '-X' is supprted but without a shortcut method, that means you'd have to use opt() method, e.g.: new CUrl(...).opt('-X', 'DELETE')

rockswang avatar Nov 15 '19 08:11 rockswang

Yeah, it work ~~~~~~~~~~ last question, can you teach me how to write a resolver ?? I have search a l ot of info but can not find it.

: ) Thanks and best regard.

k870611 avatar Nov 15 '19 09:11 k870611

you can write your own Resolver to avoid throwing a fatal Exception

sorry for misleading, Resolver is not designed to handle exceptions, it's mostly like a data converter, however it can be used to indicate a normal response (i.e. with HTTP status 200) to be biz-error and can be retried. actually CUrl does not "raise/throw" any connection Exceptions, instead, it dumps them into 'stderr', that is a CUrl.IO instance which is by default the same instance with 'stdout'.

rockswang avatar Nov 15 '19 10:11 rockswang