Jump to content

How to query api using Curl


Krlos
 Share

Recommended Posts

Hi, I don't use much curl and I need to do the following:

I have a template in PW, with the following code:

userid, reportID are received from a form in this same page.



$url = 'https://path.to.API.endpoint';
      $ch = curl_init( $url );
      $data_string  = '{
      "userid": "' . $userid . '",
      "reportID": "' . $reportID . '"
      }'; 

    curl_setopt( $ch, CURLOPT_POSTFIELDS, $data_string );
    curl_setopt( $ch, CURLOPT_HTTPHEADER, array(
    'Authorization: AUTHORIZATION KEY',
    'Content-Type:application/json'
    ));

    curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );   
    curl_setopt( $ch, CURLOPT_POST, true );   

    $result = curl_exec($ch);
    curl_close($ch);

    $obj = json_decode($result, true);

	if($obj[0]['status'] === "validated") { 

    $pdf = $obj[0]['reportURL'];

    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename="report.pdf"');
    header('Expires: 0');
    header('Cache-Control: must-revalidate');
    header('Pragma: public');
    header('Content-Length: ' . filesize($pdf));
    ob_clean();
    flush();
    readfile($pdf);
    exit;
}

The problem I have is when I run the code above I get this error:

{"statusCode":400,"error":"Bad Request","message":"Invalid cookie value"}

The API developer tells me that the error occurs because when making the query using POST I am injecting my server's cookies into his server and that is why it gives that error and the call does not go through.

He told me that the solution is to take the response from the API and use ajax to deliver the PDF report, but I don't understand how to do all this in Processwire.

Any help is very welcome.

Link to comment
Share on other sites

Is https://path.to.API.endpoint another PW instance? Or completely unrelated to your PW site?

I would first try the basics: Make a manual request with something like Postman and see if it works at all. Delete all your cookies from the API endpoint domain.

The response from the developer is somewhat confusing, and doesn't make sense. If you can't even get a response from the API due to a server error 400, you also can't deliver a PDF.

  • Like 2
Link to comment
Share on other sites

6 hours ago, dragan said:

Is https://path.to.API.endpoint another PW instance? Or completely unrelated to your PW site?

It's a remote server, not related to my PW site.

6 hours ago, dragan said:

I would first try the basics: Make a manual request with something like Postman and see if it works at all. Delete all your cookies from the API endpoint domain.

The API works from postman.

He advised to use jquery or similar to query the API, but I don't know how to do the query in curl and then send the PDF report to jquery to download the PDF file.

Basically he explained to me that the problem is that if I try to open the file from my PW site the problem occurs, but if I consult the API and pull the report URL and serve it via Ajax or other method it should work.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...