diff --git a/packages/nodes-base/nodes/Wordpress/GenericFunctions.ts b/packages/nodes-base/nodes/Wordpress/GenericFunctions.ts index 524790bdeef..a8ac2056def 100644 --- a/packages/nodes-base/nodes/Wordpress/GenericFunctions.ts +++ b/packages/nodes-base/nodes/Wordpress/GenericFunctions.ts @@ -42,11 +42,18 @@ export async function wordpressApiRequest( rejectUnauthorized = !(credentials.allowUnauthorizedCerts as boolean); } + const headers: IDataObject = { + Accept: 'application/json', + 'Content-Type': 'application/json', + }; + // Some WordPress caching plugins ignore the request method and serve a cached + // GET response to writes, which silently returns existing posts instead of creating one. + if (!['GET', 'HEAD'].includes(method)) { + headers['Cache-Control'] = 'no-cache'; + } + let options: IRequestOptions = { - headers: { - Accept: 'application/json', - 'Content-Type': 'application/json', - }, + headers, method, qs, body, diff --git a/packages/nodes-base/nodes/Wordpress/__tests__/GenericFunctions.test.ts b/packages/nodes-base/nodes/Wordpress/__tests__/GenericFunctions.test.ts index 127e76ad486..4d89c3d2f87 100644 --- a/packages/nodes-base/nodes/Wordpress/__tests__/GenericFunctions.test.ts +++ b/packages/nodes-base/nodes/Wordpress/__tests__/GenericFunctions.test.ts @@ -46,6 +46,26 @@ describe('Wordpress > GenericFunctions', () => { ); }); + it('should set Cache-Control to no-cache for write requests', async () => { + mockFunctions.helpers.requestWithAuthentication.mockResolvedValue({ data: 'testData' }); + await wordpressApiRequest.call(mockFunctions, 'POST', '/posts', {}, {}); + + expect(mockFunctions.helpers.requestWithAuthentication).toHaveBeenCalledWith( + 'wordpressApi', + expect.objectContaining({ + headers: expect.objectContaining({ 'Cache-Control': 'no-cache' }), + }), + ); + }); + + it('should not set Cache-Control for GET requests', async () => { + mockFunctions.helpers.requestWithAuthentication.mockResolvedValue({ data: 'testData' }); + await wordpressApiRequest.call(mockFunctions, 'GET', '/posts', {}, {}); + + const callArgs = mockFunctions.helpers.requestWithAuthentication.mock.calls[0][1]; + expect(callArgs.headers).not.toHaveProperty('Cache-Control'); + }); + it('should throw NodeApiError on failure', async () => { mockFunctions.helpers.requestWithAuthentication.mockRejectedValue({ message: 'fail' }); await expect(