SthisiskelvinK's avatar

Laravel PDF is creating problem

so for an application i have to send a pdf into an api then do some stuff with the pdf and then send it to another api and then return the result

problem n1:

i dont know how to get access to the pdf i sent with Postman i tried

$file = $request->get('upload_file'); 

which doesnt seem to work since i cant get the file which is called

test.pdf and has a content of

Test Dit is een test so in the meantime i do

$parser = new Parser();
$pdf = $parser->parseFile('pdf/test.pdf');
$text = pdf->getText();

problem n2:

the api i sent the transformed pdf to claims the requestbody is invalid i use Guzzle for the request

    $headers =
        [
            'headers' => [
                'Ocp-Apim-Subscription-Key' => 'xxxxxxxxxxxxxxxxxxxxxxx',
                'Accept' => 'application/json',
                'Content-Type' => 'application/json']
        ];
    $body =
        '{
            "documents": [
            {
            "language": "nl",
            "id": "1",
            "text":"' . $text . '"}
            ]
        }';

    $client = new Client();

    $response = $client->request('POST', 'https://westeurope.api.cognitive.microsoft.com/text/analytics/v2.0/KeyPhrases', $headers, $body);

it gives

`Client error: `POST
https://westeurope.api.cognitive.microsoft.com/text/analytics/v2.0/KeyPhrases
     resulted in a400 Bad Requestresponse:\n
     {"code":"BadRequest","message":"Invalid request","innerError": 
     {"code":"InvalidRequestBodyFormat","message":"Request body 
     (truncated...)\n //cant seem to format this correctly

so can anyone plz help me? and explain what i am doing wrong if they have the time

0 likes
2 replies
tisuchi's avatar
tisuchi
Best Answer
Level 70

@sthisiskelvink

If I understand you correctly.

For the first part of your question (n1), you can get the uploaded file using:

$file = $request->file('test'); // where 'test' is the 'name' of this file input

Check more: https://laravel.com/docs/master/requests#retrieving-uploaded-files

For the second part (n2), it looks like you are passing an invalid body. Try this:

$body = [
    'documents' => [
        [
            'language' => 'nl',
            'id' => '1',
            'text' => $text,
        ],
    ]
];
$headers = [
    'Ocp-Apim-Subscription-Key' => 'xxxxxxxxxxxxxxxxxxxxxxx',
    'Accept' => 'application/json',
    'Content-Type' => 'application/json' // this might not be needed since Guzzle should add this automatically
];
$client->request('POST','https://westeurope.api.cognitive.microsoft.com/text/analytics/v2.0/KeyPhrases', [
    'json' => $body,
    'headers' => $headers,
    'debug' => true, // to get more info on the error ...
]);
2 likes

Please or to participate in this conversation.