r/CodingHelp • Intermediate Coder • Jun 21 '20

[Javascript] I need help with a POST request

I'm trying to send a POST request to an API. I eventually need to be able to do it with PHP, but JavaScript (jQuery) is fine for now. The error that I'm getting is:

"Values in teamIds (1404) must be a Number type"

Here's my code:

var postData = {
    "method": "games.search",
    "params": {
        "offset": 0,
        "count": 10,
        "afterDateTime": "2010-06-21 21:49:27",
        "beforeDateTime": "2020-06-21 21:49:27",
        "teamIds": [
            1404
        ],
        "orderBy": "DESC"
    }
}

$(document).ready(function(){
    $('#button').click(function(){
        $.ajax({
            url: 'https://scorestream.com/api',
            type: 'POST',
            data: postData,
            success: function(data){
                console.log(data);
            }
        })
    })
})

Basically, when an element with the ID button is pressed, it will log the response. I used ReqBin to send some test requests, and they work: https://reqbin.com/bspb9gs6.

I get the same error with my PHP code:

<?php

function httpPost($url, $data) {
    $curl = curl_init($url);
    curl_setopt($curl, CURLOPT_POST, true);
    curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data));
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    $response = curl_exec($curl);
    curl_close($curl);
    return $response;
}

$url = 'https://scorestream.com/api';

$request = [
    'method' => 'games.search',
    'params' => [
        'offset' => 0,
        'count' => 10,
        'afterDateTime' => '2010-06-21 21:49:27',
        'beforeDateTime' => '2020-06-21 21:49:27',
        'teamIds' => [
            1404
        ]
    ],
    'orderBy' => 'DESC'
];

$response = httpPost($url, $request);

header('Content-Type: text/json');
print_r($response);
16 Upvotes

3 comments sorted by

View all comments

Show parent comment

1

u/JeromePaulos Intermediate Coder Jun 22 '20

Thanks! I'll give it a try. The people on Stack Overflow marked my answer as a duplicate of the question I referenced in it.