title: BigQuery randomnly fails with /bigquery/v2/projects/<project>/queries/>jobID-HEX 404 not found
body: <p>I am getting random failures from the Big Query API when I send it some SQL to fetch data over the REST API.</p>
  
  <p>the REST point I am using is</p>
  
  <p><a href="https://www.googleapis.com/bigquery/v2/projects/" rel="nofollow noreferrer">https://www.googleapis.com/bigquery/v2/projects/</a>${project}/queries</p>
  
  <p>Sometimes I get results and sometimes not.</p>
  
  <p>I found a similar issue on the web where the error was caused by the location parameter being defaulted to 'US', so I tried 'EU' and a couple of my queries worked but some still failed.
  The queries do take a while to run, (5 minutes) .</p>
  
  <p>I am able to run it okay in the console.</p>
  
  <p>Any help with where to look would be much appreciated</p>
  
  <p>The error I get looks like this (PS: I did not create that job id returned in the error</p>
  
  <pre><code>Query failed: &lt;!DOCTYPE html&gt;
  &lt;html lang=en&gt;
    &lt;meta charset=utf-8&gt;
    &lt;meta name=viewport content="initial-scale=1, minimum-scale=1, width=device-width"&gt;
    &lt;title&gt;Error 404 (Not Found)!!1&lt;/title&gt;
    &lt;style&gt;
      *{margin:0;padding:0}html,code{font:15px/22px arial,sans-serif}html{background:#fff;color:#222;padding:15px}body{margin:7% auto 0;max-width:390px;min-height:180px;padding:30px 0 15px}* &gt; body{background:url(//www.google.com/images/errors/robot.png) 100% 5px no-repeat;padding-right:205px}p{margin:11px 0 22px;overflow:hidden}ins{color:#777;text-decoration:none}a img{border:0}@media screen and (max-width:772px){body{background:none;margin-top:0;max-width:none;padding-right:0}}#logo{background:url(//www.google.com/images/branding/googlelogo/1x/googlelogo_color_150x54dp.png) no-repeat;margin-left:-5px}@media only screen and (min-resolution:192dpi){#logo{background:url(//www.google.com/images/branding/googlelogo/2x/googlelogo_color_150x54dp.png) no-repeat 0% 0%/100% 100%;-moz-border-image:url(//www.google.com/images/branding/googlelogo/2x/googlelogo_color_150x54dp.png) 0}}@media only screen and (-webkit-min-device-pixel-ratio:2){#logo{background:url(//www.google.com/images/branding/googlelogo/2x/googlelogo_color_150x54dp.png) no-repeat;-webkit-background-size:100% 100%}}#logo{display:inline-block;height:54px;width:150px}
    &lt;/style&gt;
    &lt;a href=//www.google.com/&gt;&lt;span id=logo aria-label=Google&gt;&lt;/span&gt;&lt;/a&gt;
    &lt;p&gt;&lt;b&gt;404.&lt;/b&gt; &lt;ins&gt;That’s an error.&lt;/ins&gt;
    &lt;p&gt;The requested URL &lt;code&gt;/bigquery/v2/projects/$project/queries/job_F6t2Lvufj01Eo1PyuUZuyUiIUwZA&lt;/code&gt; was not found on this server.  &lt;ins&gt;That’s all we know.&lt;/ins&gt;
  
  
  </code></pre>
  
  <p>I have tried with no location and also changing the location from US to EU with no luck. I'm based in the UK and the dataset I am querying has the location set to US</p>
  
  <p>below is the perl code that runs the query.</p>
  
  <p>sub query {</p>
  
  <pre><code>my ( $self, $sql, %params ) = @_;
  
  my $url
      = "https://www.googleapis.com/bigquery/v2/projects/$self-&gt;{project}/queries";
  
  my $res
      = %params
      ? $self-&gt;request( $url,
      { query =&gt; $sql, useLegacySql =&gt; \0, %params } )
      : $self-&gt;request( $url, { query =&gt; $sql, useLegacySql =&gt; \0 } );
  
  croak "Query failed: $res-&gt;{content}" unless $res-&gt;{success};
  
  my $job_id = $res-&gt;{content}{jobReference}{jobId};
  
  my $attempts;
  
  # If the result set takes longer to build than the default BigQuery
  # timeout (10 seconds), we need to poll an endpoint to gather the results.
  # We attempt this up to n times before giving up.
  until ( $res-&gt;{content}{jobComplete} ) {
      $res
          = %params
          ? $self-&gt;request( "$url/$job_id", \%params )
          : $self-&gt;request("$url/$job_id");
  
      croak "Query failed: $res-&gt;{content}" unless $res-&gt;{success};
  
      croak 'Query failed: result set took too long to return'
          if ++$attempts &gt; MAX_ATTEMPTS;
  }
  
  my @fields = map $_-&gt;{name}, @{ $res-&gt;{content}{schema}{fields} };
  
  sub {
      my @rows;
  
      for ( @{ delete $res-&gt;{content}{rows} // [] } ) {
          push @rows, \my %row;
  
          @row{@fields} = map $_-&gt;{v}, @{ $_-&gt;{f} };
      }
  
      if ( $res-&gt;{content}{pageToken} ) {
          my $url = "$url/$job_id?";
  
          $url .= HTTP::Tiny-&gt;www_form_urlencode(
              { pageToken =&gt; $res-&gt;{content}{pageToken} } );
  
          $res
              = %params
              ? $self-&gt;request( $url, \%params )
              : $self-&gt;request($url);
  
          croak "Query failed: $res-&gt;{content}" unless $res-&gt;{success};
      }
  
      @rows;
  };
  </code></pre>
  
  <p>}</p>
  
  <p>I expect the rows that are pulled returned when I run the same SQL in the console to be returned over the API in a consistent way.</p>
  
  <p>Actual result is, sometimes the REST point fails with 404 job id not found</p>