Jump to content

[Help] [Resolved] fetchRemote PATCH method does not send postData


Recommended Posts

EDIT: Solved my own issue. I forgot to add options["headers"] and add options["headers"]["Content-Type"] = "application/json".

Hi everybody,

I am currently writing an API in NodeJS that reads from and writes to a MySQL database. This is used to retrieve and update players' accounts, vehicles etcetera on my server.

To make calls to the API I am using the fetchRemote function and handle the data that comes back through a callback function. This works really well and I already have GET and POST requests that work perfectly.

I am now at a point where I'm trying to use the HTTP 'PATCH' method to update a record via the API ( e.g. update a vehicle based on the current element's colors, model, etc.), but am running into an issue where using "PATCH" as the HTTP method does not send along any data (such as the postData variable)

 

On the NodeJS side, I keep getting the message that no data was received. When I make this same call with a program like Postman, it works just fine.

Is there another way to pass the data for a patch request that is perhaps not documented, and is the patch method even supported by fetchRemote?

Lua code on the MTA server side (domains are whitelisted and the resource has the fetchRemote ACL permission)

-- The Data to send
data = {
 model = 411,
 name = 'Test Vehicle',
 color = '255,255,255,255,255,255,255,255,255,255,255,255',
 variant = '255,255'
}

-- fetchRemote options
options = {
	method = "PATCH",
    postData = string.sub(toJSON(data), 2, -2), -- string.sub to remove [] brackets
}

-- fetchRemote function calling a vehicle endpoint
fetchRemote( "http://localhost:3000/vehicles/3", options, -- URL updates vehicle with ID 3.
	function(responseData, responseInfo) -- Callback to process response
		outputDebugString('Status code: ' .. responseInfo['statusCode']) -- Output received statuscode.
    end
)

For those familiar with NodeJS and the Express framework, this is the code on the NodeJS side:

router.patch("/:id", bodyParser.json(), (req, res) => {
    const v = req.body
    const { id } = req.params
    const connection = mysql.createConnection( config.db )
    console.log(`Attempting to update vehicle with ID: ${id}`)
    console.log(`Received data:`)
    console.log(v)
    if( v.model && v.name && v.variant && v.color && v.upgrades && id) {
        connection.query(`UPDATE mta.vehicles SET name = '${v.name}', model = '${v.model}', variant = '${v.variant}}', upgrades = '${v.upgrades}', spawn_type = '${v.spawn_type}', spawn_data = '${v.spawn_data}', plate = '${v.plate}' WHERE (\`id\` = '${id}');
        `, 
            (error, results, fields) => {
                if (error) throw error;
                if( results.changedRows == 1) {
                    console.log("Updated vehicle")
                    res.status(200).send()
                }
                else {
                    console.log("Affected rows returned 0")
                    res.status(500).send()
                }
            });
    }
    else {
        console.log("Refusing to update: Missing essential data")
        res.status(500).send()
    }
});

 

 

 

Edited by xPeacefulDreams
Simpler code example.
Link to comment
  • xPeacefulDreams changed the title to [Help] [Resolved] fetchRemote PATCH method does not send postData

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
  • Recently Browsing   0 members

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