Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

question: how do I get the content in non "success" cases #6

Closed
ufobat opened this issue Jan 29, 2018 · 2 comments
Closed

question: how do I get the content in non "success" cases #6

ufobat opened this issue Jan 29, 2018 · 2 comments

Comments

@ufobat
Copy link
Contributor

ufobat commented Jan 29, 2018

I dont manage to figure out how I get the content of a HTTP response if the status-code is not < 400.

> perl6 -e 'use LibCurl::HTTP; sub MAIN(Str $url) { my $h = LibCurl::HTTP.new; try { $h.GET($url).perform; CATCH{ default {.say}}}; say $h.content }' "www.google.com/fas"
HTTP response code said error
  in method perform at /home/martin/.rakudobrew/moar-2017.09/install/share/perl6/site/sources/08E9B748943417E15B3A29F01E431CF45AD845BC (LibCurl::Easy) line 583
  in sub MAIN at -e line 1
  in block <unit> at -e line 1
> curl -v "www.google.com/fas"
*   Trying 172.217.19.68...
* TCP_NODELAY set
* Connected to www.google.com (172.217.19.68) port 80 (#0)
> GET /fas HTTP/1.1
....
< Date: Mon, 29 Jan 2018 16:35:07 GMT
< 
<!DOCTYPE html>
<html lang=en>
.... more content....

my real world example is:
I am working with an rest api that returns errors as json as well as having error codes in the http response. i cant figure out how to display those json errors from my api.

@CurtTilmes
Copy link
Owner

The LibCurl::HTTP convenience methods (GET/HEAD/PUT/etc.) all set :failonerror which throws an exception for any error status-code. You can disable that with setopt(:!failonerror)

perl6 -e 'use LibCurl::HTTP; sub MAIN(Str $url) { my $h = LibCurl::HTTP.new; try { $h.GET($url).setopt(:!failonerror).perform; CATCH{ default {.say}}}; say $h.content }' "www.google.com/fas"

@ufobat
Copy link
Contributor Author

ufobat commented Jan 29, 2018

thank you very much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants