in reply to New Bing Search Api (5)

What makes you guess that?

What parts of the documentation make you think that?

Also note that your URL as constructed is not what the documentation suggests to me. The square brackets usually indicate "optional" parts to me and your URL would include them verbatim.

Likely, your URLs should look like:

my $href= 'https://api.cognitive.microsoft.com/bing/v5.0/search?'. $My +BingJsonQuery;

Looking at the examples for the other languages easily confirms that.

From my reading of the documentation, you need to include a custom header Ocp-Apim-Subscription-Key with your key and you're all set.

Maybe now is the time to provide a complete example together with the error response you get from Microsoft instead of turning this into a guessing game.

Replies are listed 'Best First'.
Re^2: New Bing Search Api (5)
by Anonymous Monk on Nov 21, 2016 at 17:57 UTC

    Thank you for your answer. Here you can find a complete (and slightly modified) script for what I am trying to do. Api Key is not provided. If the script gets a response, it should enter the intended subrutine and I should see my nice message "I could connect to the API" and smile. But I can't get any response, so I get "Impossible retrieving info from Web".

    #!/usr/bin/perl use strict; use warnings; use LWP::UserAgent; use JSON; my $BingKey="xxx"; my $MyBingJsonQuery = "Hello"; my $href= 'https://api.cognitive.microsoft.com/bing/v5.0/search?' +. $MyBingJsonQuery; print "Connecting to Bing for: $href\n"; my $ua= LWP::UserAgent->new(); my $req = HTTP::Request->new(GET => $href); $req->header($BingKey); my $res = $ua->request($req); # Check the outcome of the response if ($res->is_success) { print "I could connect to the API\n"; my $resp= $ua->get($href); my $data = decode_json($resp->content); my @urls = map { $_->{'Url'} } @{ $data->{d}->{results} }; } else { print "Impossible retrieving info from Web\n"; }

      Have you read the documentation you linked to?

      It also shows you what kinds of errors the API could return.

      Maybe you want to show us what exact kind of error you get back?

      Printing out the full response might show you something that corresponds with the error types of the API:

      print $res->as_string;

      Maybe now is a good time to learn about HTTP and status codes and what the stuff in HTTP::Message and HTTP::Response is all about.

        Okay. I get an error message. New lesson for me. The error seems to point to my suspect. I am not passing correctly the parameters...

        HTTP/1.1 401 Access Denied Date: Mon, 21 Nov 2016 18:16:11 GMT WWW-Authenticate: AzureApiManagementKey realm="https://api.cognitive.m +icrosoft.com/bing/v5.0",name="Ocp-Apim-Subscription-Key",type="header +" Content-Length: 152 Content-Type: application/json Apim-Request-Id: a4599b0f-4402-46cd-9f3d-2d1e0f1f0199 Client-Date: Mon, 21 Nov 2016 18:16:12 GMT Client-Peer: 52.174.60.141:443 Client-Response-Num: 1 Client-SSL-Cert-Issuer: /C=US/ST=Washington/L=Redmond/O=Microsoft Corp +oration/OU=Microsoft IT/CN=Microsoft IT SSL SHA2 Client-SSL-Cert-Subject: /CN=api.cognitive.microsoft.com Client-SSL-Cipher: ECDHE-RSA-AES256-SHA384 Client-SSL-Socket-Class: IO::Socket::SSL Client-Warning: Unsupported authentication scheme 'azureapimanagementk +ey' Strict-Transport-Security: max-age=31536000; includeSubDomains; preloa +d { "statusCode": 401, "message": "Access denied due to missing subscrip +tion key. Make sure to include subscription key when making requests +to an API." }