Invalid HTTP method: PATCH

Hi,

I am trying to update a company but I get the error “Invalid HTTP method: PATCH”

as soon as I set the request method to patch

My url is:

https://api.hubapi.com/crm/v3/objects/companies/6058260265?hapikey=xxx

What could be wrong

Thanks and regards

Ad

Hi @AdKerremans

Looks like this is formatted correctly. I just tested and was able to successfully patch a company. Can you confirm that you are still having this issue?

Hi @dennisedson ,

I still get the same error.

Regards

Ad

Hey @AdKerremans

Just took a look and it appears that you successfully patched that company record twice on May 12

Hi @dennisedson ,

I just created a new company with the command

HTTPS POST request: https://api.hubapi.com/crm/v3/objects/companies?hapikey=xxx
2021/mei/18 07:46:25.525-> [portToClass] Body: {“properties”:{“zip”:“8413 NC”,“country”:“Netherlands”,“website”:“www.dexxx.nl/","address”:" Bolaan 1B",“bp_code”:"
0123082315",“city”:“Oudehorne”,“phone”:“0031523413541001”,“name”:“Rooster”,“lifecyclestage”:“customer”,“bp_balance”:“OEM”,“bp_company_type”:“mpc_nl”}}
2021/mei/18 07:46:25.843-> [portToClass] ResponseCode: 201
2021/mei/18 07:46:25.843-> [portToClass] HTTP get result: {“id”:“6090153844”,“properties”:{“address”: …
,“hs_lastmodifieddate”:“2021-05-18T05:46:25.626Z”,“hs_object_id”:“6090153844”,“lifecyclestage”:“customer”,“name”:…},“createdAt”:“2021-05-18T05:46:25.626Z”,“updatedAt”:“2021-05-18T05:46:25.626Z”,“archived”:false}

Now I am trying to update the company

HTTPS PATCH request: https://api.hubapi.com/crm/v3/objects/companies/6090153844?hapikey=xxx

String fullUrl = HubspotUtils.getParameterAlpha(param, “BASE_URL”) + urlExtension + “?hapikey=” + HubspotUtils.getParameterAlpha(param, “HAPI_KEY”);
log("HTTPS " + requestMethod + " request: " + fullUrl);
URL url = new URL(fullUrl);
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
connection.setRequestProperty(“X-HTTP-Method-Override”, “PATCH”);
connection.setRequestMethod(“POST”);

It giving the error below:

<html>
<head>
<meta http-equiv=“Content-Type” content=“text/html;charset=utf-8”/>
<title>Error 405 Method Not Allowed</title>
</head>
<body><h2>HTTP ERROR 405</h2>
<p>Problem accessing /crm/v3/objects/companies/6090153844. Reason:
<pre> Method Not Allowed</pre></p>
</body>
</html>

When using Postman it just just works.

Regards

Ad

@MichaelC , any thoughts? If it is working in postman, it probably an issue in your app.

connection.setRequestMethod(“POST”); looks suspicious :thinking:

Yep,

I agree.. are you sure it should be a post there and not a patch?

If I use PATCH I get the error:

“Invalid HTTP method: PATCH”

as soon as I set the request method to patch

Hello!

I would say something is off with your own code. You need to review it. There is nothing wrong in the Hubspot API not allowing PATCH method - we have tried it in multiple way - both through postman as well as me through GuzzleHTTP (PHP Framework) and it works.

You need to review your code - in your coding language - and to find error in your specific code langugage is not really something for the HubSpot forums but something for forums like Stack Overflow or similar.

Michael

I found the solution on stackoverflow (https://stackoverflow.com/questions/22355235/patch-request-using-jersey-client)

I pasted the relevant part below.

So I looked into Java’s HttpUrlConnection code and saw that the allowed methods are specified by an private static String[]:

 /* Adding PATCH to the valid HTTP methods */
 private static final String[] methods = {
 "GET", "POST", "HEAD", "OPTIONS", "PUT", "DELETE", "TRACE"
 };

The solution was to change this methods array using reflection:

 try {
 Field methodsField = HttpURLConnection.class.getDeclaredField("methods");
 methodsField.setAccessible(true);
 // get the methods field modifiers
 Field modifiersField = Field.class.getDeclaredField("modifiers");
 // bypass the "private" modifier 
 modifiersField.setAccessible(true);

 // remove the "final" modifier
 modifiersField.setInt(methodsField, methodsField.getModifiers() & ~Modifier.FINAL);

 /* valid HTTP methods */
 String[] methods = {
 "GET", "POST", "HEAD", "OPTIONS", "PUT", "DELETE", "TRACE", "PATCH"
 };
 // set the new methods - including patch
 methodsField.set(null, methods);

 } catch (SecurityException | IllegalArgumentException | IllegalAccessException | NoSuchFieldException e) {
 e.printStackTrace();
 }

Since methods field is static, changing its value works for any concrete instance that is extending HttpUrlConnection including HttpsUrlConnection.

Thanks for adding that solution @AdKerremans ,

Glad you got this worked out