How to call Laravel API from Python correctly

Viewed 1008

I am using Laravel 8 and Sanctum and attempting to make an API call from Python into the Laravel app.

I don't seem to be sending the request variables correctly from python as they are not being picked up as $request->variable (see "auction_item_id":null in Laravel log below) however they do show up in the request (see below Laravel log).

Is this a problem with the python request header?

Python:

import requests
import re
import json

############## LOGIN ####################
url = 'http://127.0.0.1:8082/api/apilogin'

data = {
    'email': 'user@email.com',
    'password': 'password'}
    
r = requests.post(url, data=data) 
user = json.loads(r.text)

############## MAKE REST API CALL using login token from prior call ####################

url2 = 'http://127.0.0.1:8082/api/beginlivebiddingforitemAPI'
token = "Bearer " + user["token"]
headers = {"Authorization": token, 'Accept': 'application/json'}
data = {    
    'auction_id' : 103,
    'auction_item_id' : 1224
    }
rr = requests.get(url2, data=data, headers=headers)

AuctionBidsController:

    public function beginlivebiddingforitem (Request $request){
        Log::info("AuctionBidsController@beginlivebiddingforitem", ['auction_item_id' => $request->auction_item_id]);

Api.php:

Route::post('/apilogin', 'UsersController@APIlogin');

Route::middleware(['auth:sanctum'])->group(function () {
    Route::get('/beginlivebiddingforitemAPI', 'AuctionBidsController@beginlivebiddingforitem');
});

Laravel log:

[2020-11-06 16:46:44] local.INFO: AuctionBidsController@beginlivebiddingforitem {"auction_item_id":null,"request":{"Illuminate\\Http\\Request":"GET /api/beginlivebiddingforitemAPI HTTP/1.1
Accept:          application/json
Accept-Encoding: gzip, deflate
Authorization:   Bearer 100|3EjawNPRinapHXxZvDirzyKM73AzeuJC8OTP6xOZ
Connection:      keep-alive
Content-Length:  35
Content-Type:    application/x-www-form-urlencoded
Host:            127.0.0.1:8082
User-Agent:      python-requests/2.23.0

auction_id=103&auction_item_id=1224"}}  

^^^^^^^^^^^^^^^ THEY ARE HERE ** But look at "auction_item_id":null **

Here is what a normal request looks like inside laravel:

[2020-11-06 18:34:56] local.INFO: AuctionBidsController@beginlivebiddingforitem {"auction_item_id":"1223","request":{"Illuminate\\Http\\Request":"GET /beginlivebiddingforitem?auction_id=103&auction_item_id=1223 HTTP/1.1
Accept:           */*
Accept-Encoding:  gzip, deflate, br
Accept-Language:  en-US,en;q=0.9
Connection:       keep-alive
Cookie:           laravel_cookie_consent=1; _ga=GA1.1.1339345323.1594720189; PGADMIN_INT_KEY=129e52c8-dddd-41a0-bc43-53e91dec5402; pga4_session=1c3a240a-46a1-4a9e-8e25-d489bb8ddddddddNltIXmby1M+34KepOYCsGAsY=; PGADMIN_LANGUAGE=en; _gid=GA1.1.1579327561.1604514920; _gat_gtag_UA_170463940_1=1; XSRF-TOKEN=eyJpdiI6ImIxT00yWW54REE2emVQSEpZZzZRRmc9PSIsInZhbHVlIjoiWmlCZGhia1pxeVJ0TmU4NGRBV2xMUURsS21RT0cyWGZUYddddddddddddddddddddddU9oUHg3cGQrdWZhd2gybnRCZ0xUajcwdEhuREh5OE5FcWkzV2tWb3lKOTRwUlljWld4TE9KL3BiMFp0M1dzeklmY2VhaEUiLCJtYWMiOiI0OWZhYmE3ZWM0ZjFhNTU5OWM1ZWE1MmY0MmIyMWQ3YTU1YzE3MmI0NWM1YjY0NTE0MzdhYTVmOWNmODIzMWVlIn0%3D; icollect_session=eyJpdiI6Ik5xSlAzR3NaZUR4QUhxV0RBRFBvd2c9PSIsInZhbHVlIjoianJLbFdVUDJCL2FKZEo5NTFUMElVUFJMRm9BdddddddddddddddHdYL3Arc3Avc1JTcUE5RmNPZ1J2MUVxblFUbmREcHJJeUZJaHZNVzJHTVNhR2p6Snp6M2JkQXJObE9BQU91Si9RbVIzRXMiLCJtYWMiOiI5MzcwMjc5ZWQ0MDE5ZDk2NTVmYmJjNGQ5NWYzMGY2YTVkMDI5YWQ0YTlkNGU1YTUyZTIyYzhkODU4NjNiNDA2In0%3D
Dnt:              1
Host:             127.0.0.1:8082
Referer:          http://127.0.0.1:8082/bidliveauctioneer/103
Sec-Fetch-Dest:   empty
Sec-Fetch-Mode:   cors
Sec-Fetch-Site:   same-origin
User-Agent:       Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.80 Safari/537.36 Edg/86.0.622.43
X-Csrf-Token:     wrdZoCDCVeqddddzVxAxByw183bFMuddddP4QBKy
X-Requested-With: XMLHttpRequest
X-Socket-Id:      9357.547174
Cookie: laravel_cookie_consent=1; _ga=; PGADMIN_INT_KEY=; pga4_session=; PGADMIN_LANGUAGE=; _gid=; _gat_gtag_UA_170463940_1=; XSRF-TOKEN=wrdZoCDCVeqddddzVxAxByw183bFMuddddP4QBKy; icollect_session=Qx2QddddNvlIg3ogQ596BdddducrFk4Bmi0nLA89

**note that variables occur right after the GET GET /beginlivebiddingforitem?auction_id=103&auction_item_id=1223 **

1 Answers
Related