movepixels's avatar

Accessing post data

Not sure why this is so difficcult.

I have an API endpoint on the server which accepts a POST method from 3rd party that is making a web-hook callback.

I can get the whole post string, but not the actual individual items.

public function index(Request $request) {
  $data = $request->post(); // great shows all the key/value pairs 
  
  $request->input('invoice'); //nothing
  $request->only('invoice'); // nothing

}

The actual (shortened) object looks like:

{"invoice=123456&amount1=1&amount2=0_01522&currency1=USD&txn_id=CPDJ3BBCVJWGA9ULVHBHJLTY1Q"}

How can I get the actual individual keys as needed?

$request->invoice; // returns Trying to get property of non-object
$request['invoice']; // returns Undefined index: invoice

Simply lost at something seeming so simple.

Any help greatly appreciated

0 likes
7 replies
siangboon's avatar

simply paste or screenshot the exact result will more helpful

return $request->all();

better not interpret the code/result in your own way it may confusing at least i not understanding what it mean actual object:

{"invoice=123456&amount1=1&amount2=0_01522&currency1=USD&txn_id=CPDJ3BBCVJWGA9ULVHBHJLTY1Q"}
movepixels's avatar

There is no screen shot as this is a backend API so I am simply mocking it in postman right now.

I 3rd party who sends the request via their back end provided a sample of what they send which is what I provided as a shortened example of what is being sent as a request to the API / controller endpoint.

Either or return same data.

$data = $request->all(); // either
// $data = $request->post(); // or 
return response()->json($data);

I need the individual keys.

How can i get the invoice number? the amount from the POST'd data out of $request->all();???

Thanks

Snapey's avatar

This looks like a query string. Are you sending this as post data rather than part of the query?

My understanding of post data is that its key:value pairs not key=value

movepixels's avatar

I am only going by the docs they have. I quote as " The API signature is created from the full raw POST data of your request. Using the get_callback_address function the raw request might look like: currency=BTC&version=1&cmd=get_callback_address&key=your_api_public_key&format=json "

So its not like I can look at the request in a standard url as to see exactly how they sent it.

I am simply getting the request at the API webhook end point from them. I set up a dummy api at webhook.site to see the post info and sent a test to it and this is the confusing part.

Request Details POST http://webhook.site/////*****/

Query strings (empty)

Headers content-length 360 content-type application/x-www-form-urlencoded; charset=utf-8

Form values amount1 1 currency1 USD currency2 BCT

So thats where I am at...

Thanks all for taking the time

Snapey's avatar

in your message above, can you see what you pasted? Do you need to wrap this as a code block ?

movepixels's avatar

No thats the list that i see on the webhook site when I sent the api callback to that site. Its a summary of what they received

movepixels's avatar

Sorry for confusion. Basically my API gets called from 3rd party callback webhook. But since its a webhook there is no view, I cant visually see what they are sending and the docs are pretty crap so all I could do was dump the post data to a Log to see it but it was a jumbled mess.

So I came across the webhook.site testing site where I could simply swap my endpoint to the website url and have the 3rd party send the webhook to that url. Presto I could visually see what they were sending, and how they were sending it.

I had thought they were sending a query string but infact its a form key value post. So all is good now.

I had a space before a closing " so the signatures were not matching so all is good.

Consider solved and issue was human error.

Thanks again all for the time and suggestions.

Please or to participate in this conversation.